动态生成20个按钮,程序运行时,我想当用户按了其中一个按钮就弹出对话框。
显示按钮序号

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure ButtonClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;
    implementation{$R *.dfm}
    procedure TForm1.FormCreate(Sender: TObject);
    var
      i:Integer;
      btnList:array[1..20] of Tbutton;
    begin
      for i:=1 to 20 do
      begin
        btnList[i]:=Tbutton.Create(Self);
        btnList[i].Parent:=self;
        if i<=10 then
        begin
          btnlist[i].Top:=20;
          btnlist[i].Left:=i*60;
        end
        else begin
           btnlist[i].Top:=60;
           btnlist[i].Left:=(i-10)*60;
        end;
        btnList[i].Tag:=i;
        btnList[i].Caption:=IntToStr(i);
        btnList[i].Onclick:=ButtonClick ;
      end;
    end;procedure TForm1.ButtonClick(Sender: TObject);
    begin
      showmessage(IntToStr((Sender as TButton).Tag))
    end;end.