【问题标题】:How to dynamically add buttons to TCategoryPanelGroup?如何将按钮动态添加到 TCategoryPanelGroup?
【发布时间】:2010-11-23 09:03:03
【问题描述】:

有人使用过 Delphi 2009 的 TCategoryPanelGroup 组件,特别是动态添加按钮到类别面板的经验吗?

我无法让它正常工作。要么按钮没有出现,要么对齐被搞砸了。我想做什么的基本大纲:

procedure AddButton (const Caption, Group : String);
const 
  ButtonSize = 55;
  Border = 10;
var
  CategoryPanel : TCategoryPanel;
  Button : TButton;       
begin
  CategoryPanel := FindCategoryPanel (CategoryPanelGroup, Group);
  CategoryPanel.Height := CategoryPanel.Height + ButtonSize + Border;
  Button := TButton.Create (CategoryPanel);
  Button.Parent := CategoryPanel;
  Button.Width := ButtonSize;
  Button.Height := ButtonSize;
  Button.Left := 27;
  Button.Top := CategoryPanel.ClientHeight - Border - ButtonSize;
end;

有什么提示吗?

【问题讨论】:

    标签: delphi user-interface delphi-2009 components


    【解决方案1】:

    问题是我指定顶部坐标的方式。

    我把它改成了类似的东西

    ButtonCount := CategoryPanel.ComponentCount - 2;
    Button.Top := Border + ButtonCount * (ButtonSize + Border);
    CategoryPanel.ClientHeight := Border + (ButtonCount+1) * (ButtonSize + Border);
    

    它有效。

    不知道究竟是什么导致了问题。

    【讨论】:

      【解决方案2】:

      究竟是什么问题?按钮准确显示在所需位置。

      您确定要没有文字的方形按钮吗?

      使用:

      Button.Left := 0;
      Button.Width := CategoryPanel.ClientWidth - 2;
      

      使它们的精确宽度为面板减去一个像素的偏移量。

      使用:

      Button.Width := CategoryPanel.ClientWidth; Button.Left := -1;

      创建最大宽度。它有 1 个像素的偏移量。

      [[为了公平起见,我使用 2010 年]]。

      【讨论】:

      • 而且高度也有1个像素的偏移。
      • 在实际应用中,按钮是 TButton 的子类,它们包含图像。
      • 对于确切的问题:按钮没有出现在正确的位置。它们要么根本不出现,要么彼此重叠,只有很小的偏移量。
      • 好的,我在 2010 年尝试使用普通的 TButton,它看起来还不错(关于 1 像素偏移)。能否在普通的 TPanel 上动态创建按钮?
      • 现在无法尝试(无法访问 Delphi)...我将在星期一更新。感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 2020-02-15
      相关资源
      最近更新 更多