当然可以! 如: clssEdtMy:=TEdit.Create(application) ;... clssEdtMy.OnClick:=myClick ;{事件函数}...protected ....myClick(sender:Object;...); begin .....{你的事件处理} end ;
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); 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.
如:
clssEdtMy:=TEdit.Create(application) ;...
clssEdtMy.OnClick:=myClick ;{事件函数}...protected ....myClick(sender:Object;...);
begin
.....{你的事件处理}
end ;
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);
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.
procedureTForm1.Button1Click(Sender:TObject);
varcap:string;
begin
caseTButton(sender).Tagof
1:ShowMessage(′1stButtonPressed′);
2:ShowMessage(′2ndButtonPressed′);
3:ShowMessage(′3rdButtonPressed′);
end;
end;
最后在其他两个按钮的ONCLICK事件中选择已经编好的代码。这样一来就可以实现以上功能了。