如何枚举TCategoryPanel所持有的所有控件?

WeG*_*ars 1 delphi c++builder

我有一个TCategoryPanelGroup,它包含一个TCategoryPanel(名为CatPan).CatPan包含3个列表框.

我想自动调整CatPan的大小以匹配它包含的3个列表框的高度.但CatPan没有AutoSize属性.因此,我需要枚举列表框以获得它们的高度.

但是,当我尝试枚举3个列表框时,我什么也得不到:

for i= 0 to CatPan->ControlCount-1 do CatPan[i].Height;
Run Code Online (Sandbox Code Playgroud)

因为CatPan.ControlCount返回1而不是3!似乎CapPan不是列表框的父级.可能它是这样做的,以便能够进行折叠/展开动画.

我调用lbox1-> Parent-> Name(lbox1是其中一个列表框)来查看谁是其父级,但它返回一个空字符串.

Van*_*lar 6

您缺少TCategoryPanel在其构造函数中创建TCategoryPanelSurface对象作为其子对象,因此所有控件都进入TCategoryPanelSurface对象而不进入TCategoryPanel.

在C++ Builder中,它就像:

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
Run Code Online (Sandbox Code Playgroud)

在德尔福:

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;
Run Code Online (Sandbox Code Playgroud)