Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event assignment in Delphi Interposer class

I wrote an TEdit descendant which handles the OnExit event like so

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.

on the main form of my app i'm using Interposer class like so

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.

Now i want that when the Onexit event is assigned in the main form, my own onexit implementation of the TMyCustomEdit was executed as well the code of the OnExit event of the TFormTest form. but when i run the code only the code of the TFormTest.OnExit event is executed. How i can make which both methods implementations was executed?

like image 873
Salvador Avatar asked Dec 28 '22 04:12

Salvador


1 Answers

Override DoExit. That is the method which is called when the control lost focus, and which triggers the OnExit event. Call inherited DoExit after or before, depending on your desires:

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;
like image 96
NGLN Avatar answered Jan 13 '23 14:01

NGLN