unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
BtnAry:array[0..9] of TButton;
//控件數組
//因為OnClick其實是一個屬性,下面的Procedure參數類型只要與OnClick參數類型一致即可
procedure btnClick(Sender:TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnClick(Sender: TObject);
begin
if (Sender is TButton) then
ShowMessage((Sender As TButton).Caption)
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:Integer;
begin
for i:=0 to 9do
begin
BtnAry:=TButton.Create(self);
with BtnArydo
begin
Top:=i*28+10;
name:='BtnAry'+IntToStr(i);
Parent:=Self;
Visible:=tRue;
Caption:='BtnAry'+IntToStr(i);
onclick:=btnClick;
//動態指定事件
end;
end;
end;
end.