Delphi Interposer类中的事件分配

时间:2012-03-17 18:56:33

标签: delphi delphi-xe2

我写了一个TEdit后代来处理像这样的OnExit事件

unit MyCustomEdit;

interface

uses
  Classes,
  StdCtrls;

type
 TMyCustomEdit=class(TEdit)
 private
  procedure MyExit(Sender: TObject);
 public
  constructor Create(AOwner: TComponent); override;
 end;



implementation

{ TMyCustomEdit }

uses
 Dialogs;

constructor TMyCustomEdit.Create(AOwner: TComponent);
begin
  inherited;
  OnExit:=MyExit;
end;

procedure TMyCustomEdit.MyExit(Sender: TObject);
begin
  ShowMessage('Hello from TMyCustomEdit');//this is show only when is not assignated a event handler in the onexit event.
end;

end.

在我的应用程序的主要表单上我正在使用Interposer类

unit UnitTest;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, MyCustomEdit;

type
  TEdit=class (TMyCustomEdit);
  TFormTest = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Edit1Exit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormTest: TFormTest;

implementation

{$R *.dfm}

procedure TFormTest.Edit1Exit(Sender: TObject);
begin
   ShowMessage('Hello from TFormTest');//this code is always executed
end;

end.

现在我希望当在主窗体中分配Onexit事件时,我执行了我自己的TMyCustomEdit的onexit实现以及TFormTest窗体的OnExit事件的代码。但是当我运行代码时,只执行TFormTest.OnExit事件的代码。我如何才能使两个方法实现都被执行?

1 个答案:

答案 0 :(得分:10)

覆盖DoExit。这是当控件失去焦点时调用的方法,它会触发OnExit事件。之后或之前致电inherited DoExit,具体取决于您的愿望:

procedure TMyCustomEdit.DoExit;
begin
  // Code here will run before the event handler of OnExit is executed
  inherited DoExit; // This fires the OnExit event, if assigned
  // Code here will run after the event handler of OnExit is executed
end;