我在form 中建立了一个动态按钮数组,但我不知道这么使用他们,请大虾指教
for i:=1 to strtoint 5 do
  begin
    button[i] := Tbutton.create(self);
    with button[i] do
      begin
        Parent := Self;
        top:= atop;
        left:=30;
        width:=100;
        height := 30;
        atop:=atop+30;
        caption:='cj'+inttostr(i);
      end;
  end;我建了这5个按钮后不知道怎么使用他们,如何点击他们后可以运行事件;

解决方案 »

  1.   


        with button[i] do 
          begin 
            Parent := Self; 
            top:= atop; 
            left:=30; 
            width:=100; 
            height := 30; 
            atop:=atop+30; 
            caption:='cj'+inttostr(i); 
            OnClick := ButtonClick;{procedure TForm1.ButtonClick(Sender: TObject);}
          end; 
      

  2.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}var
      buttons: array[1..5] of TButton;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage('Button "' + TButton(Sender).Caption + '" Clicked!');
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      i: integer;
      atop: integer;
    begin
      atop := 20;
      for i:=1 to 5 do
      begin
        buttons[i] := Tbutton.create(self);
        with buttons[i] do
          begin
            Parent := Self;
            top:= atop;
            left:=30;
            width:=100;
            height := 30;
            atop:=atop+30;
            OnClick := Button1Click;
            caption:='cj'+inttostr(i);
          end;
      end;
    end;end.
      

  3.   

    procedure ButtonClick(Sender: TObject);
    for i:=1 to 5 do 
      begin 
        button[i] := Tbutton.create(Owner); 
        with button[i] do 
          begin 
            Parent := Self; 
            top:= atop; 
            left:=30; 
            width:=100; 
            height := 30; 
            atop:=atop+30; 
            caption:='cj'+inttostr(i); 
            OnClick := ButtonClick;
          end; 
      end;
      

  4.   


    procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(TButton(Sender).Caption+'  test');
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      i: integer;
      atop: integer;
    begin
      atop := 20;
      for i:=1 to 5 do
      begin
        buttons[i] := Tbutton.create(self);
        with buttons[i] do
          begin
            Parent := Self;
            top:= atop;
            left:=30;
            width:=100;
            height := 30;
            atop:=atop+30;
            caption:='cj'+inttostr(i);
            OnClick := Button1Click;
           
          end;
      end;
    end;
      

  5.   


    每个button可以根据不同的tag进行click处理。
      

  6.   

    设置每个按钮的tag,然后自己写个事件。把按钮的click事件定义到定义的事件里就好了。
      

  7.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}var
      buttons: array[1..5] of TButton;procedure TForm1.Button1Click(Sender: TObject);
    begin
      //如果每個Button都執行一樣的操作
      ShowMessage('Button "' + TButton(Sender).Caption + '" Clicked!');
      //如果每個Button每個都有各自的功能
      case TButton(Sender).Tag of
      1:showmessage('1');
      2:showmessage('2');
      3:showmessage('3');
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      i: integer;
      atop: integer;
    begin
      atop := 20;
      for i:=1 to 5 do
      begin
        buttons[i] := Tbutton.create(self);
        with buttons[i] do
          begin
            Parent := Self;
            top:= atop;
            left:=30;
            width:=100;
            height := 30;
            atop:=atop+30;
            OnClick := Button1Click;
            caption:='cj'+inttostr(i);
            Tag:=i;
          end;
      end;
    end;end.