2012-03-17 64 views
4

我寫了一個TEDIT後代負責處理我的應用程序的主要形式,我使用的內插器類,像這樣事件指派

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. 

像這樣

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. 

中的OnExit事件現在我希望在主窗體中分配Onexit事件時,我自己執行的TMyCustomEdit的onexit實現以及TFormTest窗體的OnExit事件的代碼被執行。但是當我運行代碼時只執行TFormTest.OnExit事件的代碼。我怎樣才能使這兩個方法的實現被執行?

回答

10

覆蓋DoExit。這是控制失去焦點時會調用的方法,並且會觸發事件。之後或之前致電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;