Delphi: 如何制作 TButtons 3x3?
Delphi: How to make TButtons 3x3?
我创建了多个 TButton。
问题是我希望创建的按钮看起来像 3x3。
怎么做?
注意:按钮会更多!
我的代码:
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;
我用 Lazarus 测试了代码(抱歉,我现在还没有 Delphi),但它应该适用于您的版本。如果不是 - 将 Top 和 Left 替换为 Position
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;
既然你提到了使用 TGridLayout,这里有一些代码展示了如何修改你的代码,以类似于你的屏幕截图的方式将一些 TButtons 放在一起:
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;
我创建了多个 TButton。
问题是我希望创建的按钮看起来像 3x3。
怎么做? 注意:按钮会更多!
我的代码:
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;
我用 Lazarus 测试了代码(抱歉,我现在还没有 Delphi),但它应该适用于您的版本。如果不是 - 将 Top 和 Left 替换为 Position
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;
既然你提到了使用 TGridLayout,这里有一些代码展示了如何修改你的代码,以类似于你的屏幕截图的方式将一些 TButtons 放在一起:
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;