Delphi: How to make TButtons 3x3?

为君一笑 提交于 2019-12-12 04:30:57

问题


I've created multi TButtons.

Problem is I'd like created buttons looks like 3x3.

How to do that? Note: Buttons will be more!

My code:

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;

回答1:


Since you mentioned using a TGridLayout, here is some code which show how to modify your code to lay out some TButtons in one, in a manner resembling your screenshot:

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;



回答2:


I tested the code with Lazarus (sorry, I haven't Delphi right now) but it should work with your version. If not - replace Top and Left with 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;   



来源:https://stackoverflow.com/questions/42902299/delphi-how-to-make-tbuttons-3x3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!