代码之家  ›  专栏  ›  技术社区  ›  IceCold

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

  •  1
  • IceCold  · 技术社区  · 7 年前

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

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

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

    我打电话给lbox1->父级->Name(lbox1是列表框之一)查看谁是其父级,但它返回一个空字符串。

    1 回复  |  直到 7 年前
        1
  •  6
  •   Remy Lebeau    6 年前

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

    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
    

    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;