OnClick事件覆盖 [英] OnClick event override

查看:323
本文介绍了OnClick事件覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义类女巫,它是从TButton派生的:

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;

但是当我单击按钮时,它没有触发OnClickResult,我在做什么错?是否可以替代 OnClick事件处理程序,还是应该隐藏它并使其成为OnResultClick事件?

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?

推荐答案

编写组件时,则不应使用事件处理程序来实现自定义行为。相反,您应该重写调用这些事件处理程序的代码。在这种情况下,无需设置 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 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.

如果您希望组件用户看到不同的'OnClick'方法,则必须实现这个自己,就像

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;

这篇关于OnClick事件覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆