如何动态创建 50 个Button?每行a(例如:5)个,共b(例如:10)列,也就是矩形排列(5*10).如图:* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *只要是想知道如何设置动态创建的按钮的Left/Top的数值;

解决方案 »

  1.   

    procedure TForm1.FormCreate(Sender: TObject);
    var
      i,j: Integer;
    const
      ButtonName = 'MyButton';
    begin
      for i := 1 to 10 do
      for j := 1 to 5 do
      begin
        TButton.Create(Self).Name := ButtonName + IntToStr(i) + IntToStr(j);
        with TButton(FindComponent(ButtonName + IntToStr(i) + IntToStr(j))) do
        begin
          Left := 10 + j*100;
          Top := i * 40;
          Parent := self;
        end;
      end;
    end;
      

  2.   

    procedure TForm1.FormCreate(Sender: TObject);
    var i,j:integer;
        Button:TButton;
    begin
      for i:=0 to 49 do
      begin
        for j:=0 to 4 do
        begin
          Button:=TButton.Create(Self);
          Button.Name:='Button'+IntToStr(i+1)+IntToStr(j+1);
          Button.Caption:='Button'+IntToStr(i+1)+','+IntToStr(j+1);
          Button.Left:=j*100;
          Button.Top:=i*50;
          Button.Parent:=Self;
        end;
      end;
    end;