Search code examples
delphifiremonkeydelphi-xe8

Delphi: How to make TButtons 3x3?


I've created multi TButtons. Created Buttons

Problem is I'd like created buttons looks like 3x3. 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;

Solution

  • 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;