如何向 Delphi TFlowLayout动态添加控件

inkz8wg9  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(154)

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

procedure AddLabel;
var
  LLabel: TLabel;
begin
  LLabel := TLabel.Create(Self);
  LLabel.Parent := Layout1;  // LABEL SHOWS OK
  LLabel := TLabel.Create(Self);
  LLabel.Parent := FlowLayout1;  // LABEL DOESN'T SHOW
end;

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

procedure AddLabel;
var
  LLabel: TLabel;
begin
  LLabel := TLabel.Create(Self);
  FlowLayout1.Controls.Add(LLabel);  // THROWS AV
end;


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

goqiplq2

goqiplq21#

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

type
  TForm1 = class(TForm)
    flwlytMain: TFlowLayout;
    btnStart: TButton;
    procedure btnStartClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.btnStartClick(Sender: TObject);
var
  MyLabel: TLabel;
  MyButton : TButton;
  ControlList: TControlList;
begin
  MyLabel := TLabel.create(self);
  MyLabel.Parent := flwlytMain  ;
  MyLabel.AutoSize := true;
  MyLabel.Text := 'Test   ';
  MyLabel.Font.Size := 48;
  MyLabel.Visible := True;
  MyButton := TButton.Create(self);
  MyButton.Text := 'Another test';
  MyButton.Width := 120;
  MyButton.parent := flwlytMain;
  MyButton.OnClick := btnStartClick;
end;

字符串

相关问题