如何向 Delphi TFlowLayout动态添加控件

inkz8wg9  于 2024-01-07  发布在  其他
关注(0)|答案(1)|浏览(519)

如何在运行时向TFlowLayout添加控件?设置父控件对于TLayout来说已经足够了,但对于TFlowLayout似乎不起作用。下面的代码解释了我的问题:

  1. procedure AddLabel;
  2. var
  3. LLabel: TLabel;
  4. begin
  5. LLabel := TLabel.Create(Self);
  6. LLabel.Parent := Layout1; // LABEL SHOWS OK
  7. LLabel := TLabel.Create(Self);
  8. LLabel.Parent := FlowLayout1; // LABEL DOESN'T SHOW
  9. end;

字符串
如果我这样做,我得到一个AV:

  1. procedure AddLabel;
  2. var
  3. LLabel: TLabel;
  4. begin
  5. LLabel := TLabel.Create(Self);
  6. FlowLayout1.Controls.Add(LLabel); // THROWS AV
  7. end;


我知道GridPanel有RowCollections和ColumnCollections,但我不知道TFlowLayout是怎么回事。

goqiplq2

goqiplq21#

下面的代码在10.4上工作正常。在你的情况下,标签可能不会显示,因为没有文本分配给Text属性。当你将TLabel拖到窗体设计时IDE自动将文本分配给他。
我测试时没有将文本分配给标题,瞧,标签已经不在这里了。

  1. type
  2. TForm1 = class(TForm)
  3. flwlytMain: TFlowLayout;
  4. btnStart: TButton;
  5. procedure btnStartClick(Sender: TObject);
  6. private
  7. { Private declarations }
  8. public
  9. { Public declarations }
  10. end;
  11. var
  12. Form1: TForm1;
  13. implementation
  14. {$R *.fmx}
  15. procedure TForm1.btnStartClick(Sender: TObject);
  16. var
  17. MyLabel: TLabel;
  18. MyButton : TButton;
  19. ControlList: TControlList;
  20. begin
  21. MyLabel := TLabel.create(self);
  22. MyLabel.Parent := flwlytMain ;
  23. MyLabel.AutoSize := true;
  24. MyLabel.Text := 'Test ';
  25. MyLabel.Font.Size := 48;
  26. MyLabel.Visible := True;
  27. MyButton := TButton.Create(self);
  28. MyButton.Text := 'Another test';
  29. MyButton.Width := 120;
  30. MyButton.parent := flwlytMain;
  31. MyButton.OnClick := btnStartClick;
  32. end;

字符串

展开查看全部

相关问题