Search code examples
delphic++buildervcltcategorypanelgroup

How to enumerate all controls hold by a TCategoryPanel?


I have a TCategoryPanelGroup which contains a single TCategoryPanel (named CatPan). CatPan contains 3 listboxes.

I want to automatically resize the CatPan to match the height of the 3 listboxes it contains. But CatPan does not have an AutoSize property. Therefore, I need to enumerate the listboxes to get their height.

However, I get nothing when I try to enumerate the 3 listboxes:

for i= 0 to CatPan->ControlCount-1 do CatPan[i].Height;

because CatPan.ControlCount returns 1 instead of 3!!! It seems that the CapPan is not the parent of the listboxes. Probably it is doing so in order to be able to do the collapse/expand animation.

I called lbox1->Parent->Name (lbox1 is one of the listboxes) to see who is its parent but it returns an empty string.


Solution

  • You are missing that TCategoryPanel creates TCategoryPanelSurface Object as its child in it's constructor, therefore all controls go into TCategoryPanelSurface Object and NOT into TCategoryPanel.

    In C++ Builder it goes like:

    ShowMessage(ListBox1->Parent->ClassName()); //you can see actual parent class here
    TCategoryPanelSurface  * Surface;
    Surface = dynamic_cast <TCategoryPanelSurface *> (CatPan->Controls[0]);
    ShowMessage(Surface->ControlCount);
    ShowMessage(Surface->Controls[0]->Name); //you should use loop here to iterate through controls
    

    In Delphi:

    var
      Surface: TCategoryPanelSurface;
      I: Integer;
    begin
      Surface := CatPan.Controls[0] as TCategoryPanelSurface;
      for I := 0 to Surface.ControlCount - 1 do
      begin
        ShowMessage(Surface.Controls[I].Name);
      end;
    end;