Delphi自定义事件TNotifyEvent用法
自定义事件定义的方法和定义属性差不多,只是类型时TNotifyEvent。
TNotifyEvent是默认事件,其定义为:
TNotifyEvent
= procedure(Sender: TObject)
示例一(仅列出相当代码):
type
TParent=class(TObject)
private
FTimeOut:TNotifyEvent;//事件的方法指针
protected
procedure
DoTimeOut;dynamic; //调度方法,用于关联事件。
public
published
property
OnTimeOut:TNotifyEvent read FTimeOut write FTimeOut;
property
OnClick;
property
OnDblClick;
end;
implementation
//事件调度函数,将外部的事件处理函数和该类的事件方法指针联系起来
procedure
TTimeCount.DoTimeOut;
begin
if
Assigned(FTimeOut) then
FTimeOut(Self);
end;
end;
示例二(仅列出相当代码):
TwfcNode =
class (TwfcShape)
private
FOnClick:
TNotifyEvent;
procedure
SetOnClick(const Value: TNotifyEvent);
public
property OnClick: TNotifyEvent read FOnClick write SetOnClick;
end;
procedure
TwfcNode.SetOnClick(const Value: TNotifyEvent);
begin
fOnClick
:= Value;
end;