OnClick 事件覆盖

发布于 2024-11-07 09:42:52 字数 847 浏览 0 评论 0原文

我的自定义类女巫是从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2024-11-14 09:42:52

编写组件时,不应使用事件处理程序来实现自定义行为。相反,您应该重写调用这些事件处理程序的代码。在这种情况下,请忘记设置 OnClick。相反,只需添加

public
  procedure Click; override;

到您的类声明中,并实现

procedure TLoginButton.Click;
begin
  inherited; // call the inherited Click method.
  // Do something new.
end;

事件处理程序供开发人员使用组件使用。组件编写者不应该自己使用它们。

如果您希望组件用户看到不同的“OnClick”方法,您必须自己实现它,例如

type
  TLoginResultEvent = procedure(Sender: TObject; LoginResult: boolean) of object;

...

TLoginButton = class(TButton)
private
  FOnClick: TLoginResultEvent;
...
public
  procedure Click; override;
...
published
  property OnClick: TLoginResultEvent read FOnClick write FOnClick;
...

procedure TLoginButton.Click;
begin
  inherited;
  if Assigned(FOnClick) then
    FOnClick(Self, true); // or false...
end;

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 add

public
  procedure Click; override;

to your class declaration, and implement

procedure TLoginButton.Click;
begin
  inherited; // call the inherited Click method.
  // Do something new.
end;

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

type
  TLoginResultEvent = procedure(Sender: TObject; LoginResult: boolean) of object;

...

TLoginButton = class(TButton)
private
  FOnClick: TLoginResultEvent;
...
public
  procedure Click; override;
...
published
  property OnClick: TLoginResultEvent read FOnClick write FOnClick;
...

procedure TLoginButton.Click;
begin
  inherited;
  if Assigned(FOnClick) then
    FOnClick(Self, true); // or false...
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文