如何在程序运行时动态创建多个按钮,使运行的效果为出现在一个固定的位置,并且这多个按钮的安排是几行几列的一个矩形,具体如下:
Botton1 Botton2 Botton3 Botton4
Botton5 Botton6 Botton7 Botton8
Botton9 Botton10 Botton11 Botton12
高手帮帮忙啊,我知道可以用for循环或者数组,但是我不会写代码,老是错,达不到效果啊,急!!!

解决方案 »

  1.   

    procedure TForm1.FormCreate(Sender: TObject);
    var aButton : TButton;
        i, x, y : integer;
    begin
      x := 0; y := 0;
      for i := 1 to 12 do
        begin
           aButton:=TButton.Create(self);
           x := x + 100;
           with  aButton do
            begin
              if i mod 4 =1 then
                begin
                  x := 0;
                  y := y + 30;
                end;
              Left := x;
              Top := y;
              Parent := Form1;
              Caption := 'Button'+IntToStr(i);
            end
        end;
    end;
      

  2.   

    改变一下就可以了。
    for i := 1 to 12 do // 这里控制Button的总共个数
    ....
    if i mod 4 =1 then // 这里控件一行Button的个数
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Btns:array[0..11] of TButton;
      I,J :Integer;
      X,Y :Integer;
    begin
    X:=0;
    Y:=0;
    for I:=0 to Length(Btns)-1 do
      begin
        if (I mod 4)=0 then
          begin
            Y:=Y+40;
            X:=0;
          end;
        X:=X+100;
        Btns[I]:=TButton.Create(nil);
        Btns[I].Parent:=Form1;
        Btns[I].Width:=75;
        Btns[I].Height:=25;
        Btns[I].Left:=X;
        Btns[I].Top:=Y;
        Btns[I].Name:='Button'+InttoStr(I+1);
      end;
    end;
      

  4.   

    如果是按我的方法创建,可以用以下代码来清除界面上创建的按钮,用老之的变量而不用数组的话自己改下代码就可以了
    procedure TForm1.btn2Click(Sender: TObject);
    var
      I:Integer;
    begin
    I:=0;
    while (I<=ComponentCount-1) and (I>=0) do
      begin
        if (Components[i].ClassType=TButton) and (leftstr(TButton(Components[i]).Name,6)='Button')  then
          begin
            Components[I].Destroy;
            I:=I-1;
          end;
          I:=I+1;
      end;
    Update;
    end;
      

  5.   

    不是很懂啊,我现在用了liangqingzhi(老之)的方法,具体怎么改啊,555555555555,发现自己好笨
      

  6.   

    条件的后面改成(leftstr(TButton(Components[i]).Caption,6)='Button')就可以了,不过要把你的不是动态创建的按钮的caption设置为非button开头的
      

  7.   

    引用strutils单元啊,这个也要我说啊