我想在FORM上添加一个Button控件数组,如何做?高分!!!!!

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure MyClick(Sender:TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      a: array[1..100] of tbutton;implementation{$R *.dfm}procedure TForm1.MyClick(Sender: TObject);
    var
      i: integer;
      nam: string;
    begin
      i := strtoint(Trim(Copy(TButton(Sender).Name,7,3)));
      TButton(Sender).Visible := not TButton(Sender).Visible;
      i := i + 1;
      nam := 'button' + inttostr(i);
      if i <= 100 then
        TButton(FindComponent(nam)).Visible := not TButton(FindComponent(nam)).Visible
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      i: integer;
    begin
      for i := 1 to 100 do
      begin
        a[i] := tbutton.Create(self);
        with a[i] do
        begin
          Top := (i-1) div 10 * 20;
          Left := (i-1) mod 10 * 20;
          Height := 20;
          Width := 20;
          Name := 'button' + inttostr(i);
          Caption := '';
          Parent := Form1;
          OnClick := MyClick;
        end;
      end;
    end;end.
      

  2.   

    代码copy一下,下拉设置From的onCreate事件为TForm1.FormCreate
    运行即可看到效果