构件所在的窗体一般来说是它的Owner, 静态建立的控件都是如此Form类除外;
一个窗体的OnClick事件是否存在是通过Assigned函数检测的;
调用Form的OnClick事件可以用Self.FormCreate(self);
例如:
1. 新建一个APP
2. 放上两个Button, 令Button1.caption:='Assign'
3. 写一个FormCreate过程
4. 创建Button1, Button2的OnClick事件(其中的self可以用)
TForm(Button1.Owner)代替; 代码如下:
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
showmessage('Hi DelphiSky!');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Assigned(Self.OnCreate) then
begin
Self.OnCreate:=nil;
Button1.Caption:='Assign'
end
else
begin
Self.OnCreate:=FormCreate;
Button1.Caption:='UnAssign';
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Assigned(Self.OnCreate) then
Self.OnCreate(self)
else
showmessage('Please use Button1 to assign'+
' the OnClick to the form, then try this button again');
end;