delphi - 如何将运行时创建的按钮添加到数组中?

标签 delphi

如果这个问题看起来很愚蠢,我很抱歉,但是在过去的几个小时里我似乎无法正常使用我的头脑。

我有记录,

type
  TMain = record
    Sub:Array of TSubMain; //another record
    Button:TsSpeedButton; //this is what we need!
  end;

变量

 Main:Array of TMain;

及功能:

procedure TFrameSkilLView.CreateButtons(MainBtns,SubMainBtns:byte;title:Array of    string);
var i,t,l,w,h:word;
section:string;
begin
  l := 41; t:= 57; w := 58; h := 25;
  section := 'TOOLBTN_SKILLS_MAIN';
  for i := 0 to MainBtns + subMainBtns - 1 do
  with TsSpeedButton.Create(nil) do begin
    Width := w; Height := h; Top := t; Left := l;
    if(i = 0) then SkinData.SkinSection := section + '_C' else skindata.SkinSection := section;
    caption := title[i];
    Parent := Self;
    inc(l,w+4);
    if(i = MainBtns - 1) then begin
      l := 52; t := 83; w := 64; h := 28;
      section := 'TOOLBTN_SKILLS_SUBMAIN';
    end;
  end;
end;

让我们重点关注循环“for i := 0 to MainBtns + subMainBtns - 1”。我想将下面创建的按钮添加到上面创建的名为“Main:Array of Tmain”的数组中。

它应该看起来像这样:

for i:=0 to X do
with TsSpeedButton.Create(nil) do begin
Main[i] := this; //where this is the created sSpeedButton.

然而,这段代码甚至无法编译,所以我要求一种可行的方法来完成我想要做的事情。

谢谢。

最佳答案

首先,“这个”是 C++,而不是 Pascal。 Delphi 版本是“Self”。其次,您不能通过名称引用 with-ed 对象。你最好根本不使用 with 。尝试这样的事情:

for i:=0 to X do
begin
  tempButton := TsSpeedButton.Create(nil);
  Main[i] := tempButton;
  //whatever else
end;

关于delphi - 如何将运行时创建的按钮添加到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1228671/

相关文章:

Delphi:构造不调用重写的虚拟构造函数

delphi - 如何使用 jenkins 为 Delphi 中的项目获得自动化构建服务器

delphi - Delphi 有没有好的类似 Pascal 的脚本语言?

delphi - Windows Vista 上的应用程序看不到 Shift+Ctrl+0 组合键

delphi - 当变量未初始化时会产生简单的代码

delphi - 提高写入文件的速度

delphi - 如何在Delphi FireMonkey中调整按钮大小以适合文本?

delphi - 保护田地是个好主意吗?

delphi - 如何安装Delphi 10.4 Sydney命令行编译器(在Windows Server 2016上)?

delphi - Windows.GetRValue 接受基数,但 TColor 是整数