Recent

Author Topic: [SOLVED] How to assign default event handler?  (Read 7867 times)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 914
    • Lebeau Software
Re: How to assign default event handler?
« Reply #15 on: March 09, 2021, 07:23:40 pm »
What I'm looking for is assigning this automatically, within constructor, etc. I will be able to do this if I define a descendant class, like following example:

Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. TMyEdit = Class(TEDIT)
  4.      Procedure DoClick(Sender:TObject);
  5.      constructor Create(Owner: TObject); override;
  6. End;
  7.  
  8. implementation
  9.  
  10. constructor TMyEdit.Create(Owner: TObject);
  11. begin
  12.       inherited Create(Owner);
  13.       OnClick:= DoClick;
  14. end;

DoClick() is a virtual method that fires the OnClick event.  The whole point of having such a virtual method is so that you don't have to assign an event handler at all, just override the virtual method instead, eg:

Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. type
  4.   TMyEdit = class(TEdit)
  5.     procedure DoClick; override;
  6.   end;
  7.  
  8. implementation
  9.  
  10. procedure TMyEdit.DoClick;
  11. begin
  12.   // do your work here as needed...
  13.   // if desired, call inherited to fire the OnClick event...
  14. end;

Most components have virtual event firers for exactly this reason.

But in this case, I think I have to register TMyEdit on the component pallette. I'm looking for ways to do this without registering new component --- only using helpers.

Then you can define your class to be an interposer instead.  An interposer is a class that derives from another class in a different unit, and has the same name as the class it is deriving from, eg:

Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. type
  4.   TEdit = class(StdCtrls.TEdit)
  5.     procedure DoClick; override;
  6.   end;
  7.  
  8. implementation
  9.  
  10. procedure TEdit.DoClick;
  11. begin
  12.   // do your work here as needed...
  13.   // if desired, call inherited to fire the OnClick event...
  14. end;

This way, if you place your unit in another unit's uses clause after the StdCtrls unit, then the compiler will use your TEdit class automatically, instead of using the standard TEdit class, eg:


Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., StdCtrls, ..., MyUnit;
  3.  
  4. type
  5.   TSomeForm = class(TForm)
  6.     Edit1: TEdit; // <=-- will use MyUnit.TEdit, not StdCtrls.TEdit!
  7.     ...
  8.   end;
« Last Edit: March 09, 2021, 07:29:29 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

egsuh

  • Hero Member
  • *****
  • Posts: 712
Re: How to assign default event handler?
« Reply #16 on: March 10, 2021, 03:41:54 am »
@Remy Lebeau,

Really thank you for our explanations. They made a lot of things clear (within my head).

Reality is TEdit.Click instead of TEdit.DoClick, but anyway I could interpose it.

Overing virtual methods does not work in helper types --- I have to assign Edit1.Onclick if I use helper. But anyway I have a few options that will make codes much neater.   

I'll buy you a glass of Remy Martin XO if I see you^^

egsuh

  • Hero Member
  • *****
  • Posts: 712
Re: [SOLVED] How to assign default event handler?
« Reply #17 on: March 10, 2021, 03:54:29 am »
FYI,
I tested followings --- not using separate unit, but re-declaring within the same unit.


Code: Pascal  [Select][+][-]
  1. unit fmain;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls;
  7.  
  8. type
  9.   TEdit = class(StdCtrls.TEdit)
  10.       procedure Click; override;
  11.   end;
  12.  
  13.   TForm1 = class(TForm)
  14.       Edit1: TEdit;
  15.   end;
  16.  
  17. var
  18.   Form1: TForm1;
  19.  
  20. implementation
  21.  
  22. {$R *.lfm}
  23.  
  24. procedure TEdit.Click;
  25. begin
  26.    // inherited Click;
  27.    showmessage('Hello');
  28. end;
  29.  
  30. end.
  31.  

I think this is quite convenient, if used carefully within frames, etc.

 

TinyPortal © 2005-2018