怎么做? 注意:按钮会更多!
我的代码:
procedure TForm1.CreateButtonsClick(Sender: TObject);
var
i:integer;
B: TButton;
begin
for i:= 1 to 7 do
begin
B := TButton.Create(Self);
B.Text := Format('Button %d', [i]);
B.Parent := Self;
B.Height := 23;
B.Width := 100;
B.Position.X:=25 + i* 105;
B.Position.Y:=70;
end;
end;
答案 0 :(得分:3)
我用Lazarus测试了代码(对不起,我现在还没有Delphi)但它应该适用于你的版本。如果不是 - 将顶部和左侧替换为位置
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
B: TButton;
begin
for i := 0 to 13 do
begin
B := TButton.Create(Self);
B.Caption := Format('Button %d', [i + 1]);
B.Parent := Self;
B.Height := 23;
B.Width := 100;
B.Left := 25 + (i mod 3) * 105;
B.Top := 70 + (i div 3) * 70;
end;
end;
答案 1 :(得分:3)
由于您提到使用TGridLayout,这里有一些代码显示如何修改代码以将一些TButton布置在一个中,方式类似于屏幕截图:
procedure TForm1.AButtonClick(Sender: TObject);
begin
ShowMessage(TButton(Sender).Text);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CreateButtons;
end;
procedure TForm1.CreateButtons;
var
i:integer;
B: TButton;
begin
GridLayout1.ItemWidth := 100;
GridLayout1.ItemHeight := 23;
for i:= 1 to 7 do
begin
B := TButton.Create(Self);
GridLayout1.AddObject(B);
B.Text := Format('Button %d', [i]);
B.Margins.Left := 5;
B.Margins.Top := 5;
B.OnClick := AButtonClick;
//B.Parent := Self;
//B.Height := 23;
//B.Width := 100;
//B.Position.X:=25 + i* 105;
//B.Position.Y:=70;
end;
end;