Search code examples
delphilayoutvcl

Auto layout of vertical panels in Delphi


I have been away from Delphi for a few years, doing web apps.

I have gotten used, in HTML of just declaring successive <div> ... </div> and having them placed below each other, with spacing provided by CSS.

I want to do a Delphi app where I have a collection of data, let's say it's data about movies. Initially, I would have a panel for each , showing only the title. If you click one, it will expand to show actors, director, plot summary, etc. Click another and the first will shrink & the new one expand (max one expanded; click that one again to shrnkk it).

So, I have two layout problems: 1) to calculate the initial .top of each panel and 2) to re-calculate as different panels are clicked.

I am quite capable, of course, of coding all of this manually, but it seems tedious and error prone.

Is there an accepted way to do this? A VCL component that comes with Delphi? Should I be asking on https://softwarerecs.stackexchange.com/ for a 3rd party VCL component?


Solution

  • The closest VCL control is probably TCategoryPanelGroup, which is composed of vertically aligned expandable panels (TCategoryPanel).

    The control does not have an 'autocollapsepanels' or 'maxexpandedpanelcount' property, but you can use some simple code to achieve the required behavior. E.g. the below OnExpand event handler, if attached to all category panels in the group, will cause an expanded panel to close others.

    procedure TForm1.CategoryPanelExpand(Sender: TObject);
    var
      I: Integer;
    begin
      if Sender is TCategoryPanel then
        for I := 0 to CategoryPanelGroup1.Panels.Count - 1 do
          if CategoryPanelGroup1.Panels[i] <> Sender then
            TCategoryPanel(CategoryPanelGroup1.Panels[i]).Collapse;
    end;
    


    You can set AlignWithMargins property of your panels to true, then adjust spacing by using the Margins properties of the panels.