OnClick 事件覆盖
我的自定义类女巫是从 TButton 派生的:
TLoginResultEvent = procedure (Sender: TObject; LoginResult: boolean) of object;
TLoginButton = class(TButton)
private
fLogin: TLoginChooser;
fOnClick: TLoginResultEvent;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure OnClickResult(Sender: TObject; LoginResult: boolean);
published
property Login: TLoginChooser read fLogin write fLogin;
property OnClick: TLoginResultEvent read fOnClick write fOnClick;
end;
在构造函数中我添加了:
constructor TLoginButton.Create(AOwner: TComponent);
begin
inherited;
fOnClick := OnClick;
OnClick := OnClickResult;
end;
但是当我单击按钮时,它没有触发 OnClickResult,我做错了什么?是否可以“覆盖”OnClick 事件处理程序,或者我应该隐藏它并创建例如 OnResultClick 事件?
I have my custom class witch is derived from TButton:
TLoginResultEvent = procedure (Sender: TObject; LoginResult: boolean) of object;
TLoginButton = class(TButton)
private
fLogin: TLoginChooser;
fOnClick: TLoginResultEvent;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure OnClickResult(Sender: TObject; LoginResult: boolean);
published
property Login: TLoginChooser read fLogin write fLogin;
property OnClick: TLoginResultEvent read fOnClick write fOnClick;
end;
in constructor I added:
constructor TLoginButton.Create(AOwner: TComponent);
begin
inherited;
fOnClick := OnClick;
OnClick := OnClickResult;
end;
But when I click on the button it's not firing OnClickResult, what am I doing wrong? Is it possible to "override" OnClick event handler or should I hide it and make for example OnResultClick event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编写组件时,不应使用事件处理程序来实现自定义行为。相反,您应该重写调用这些事件处理程序的代码。在这种情况下,请忘记设置
OnClick
。相反,只需添加到您的类声明中,并实现
事件处理程序供开发人员使用组件使用。组件编写者不应该自己使用它们。
如果您希望组件用户看到不同的“OnClick”方法,您必须自己实现它,例如
When writing components, you should not use the event handlers to implement custom behaviour. Instead you should override the code calling these event handlers. In this case, forget about setting
OnClick
. Instead, simply addto your class declaration, and implement
The event handlers are there to be used by the developer using the component. The component writer should not use them herself.
If you want the component user to see a different 'OnClick' method, you have to implement this yourself, like