Lazarus

Programming => General => Topic started by: pcurtis on September 26, 2020, 12:41:53 pm

Title: Default event
Post by: pcurtis on September 26, 2020, 12:41:53 pm
Hi All,

I am designing a visual component and was wondering how do I set the default designtime double click event?

Thanks in advance.
Title: Re: Default event
Post by: howardpc on September 26, 2020, 01:06:32 pm
If your component descends from TControl, you can override its DblClick method.
Title: Re: Default event
Post by: furious programming on September 26, 2020, 01:19:40 pm
[…] how do I set the default designtime double click event?

This sounds as if creating a component instance (in the designer) generates a default event, visible in the Object Inspector window. It sounds incorrect because events are intended for the end user, so don't do something like the default event.

In that case, you either override the existing message handler (if any exists in the base class) or write your own message handler. If a message handler exists in the base class, still you can write your own — it depends on your needs.
Title: Re: Default event
Post by: lucamar on September 26, 2020, 02:15:52 pm
I don't know how to do it but I think he means setting the event whose handler is added by default when you double-click the control in the form designer.

For example, for a TButton it generates an OnClick handler while for a TEdit it's for OnChange; how is that determined?

Is that it, pcurtis?
Title: Re: Default event
Post by: pcurtis on September 26, 2020, 03:17:38 pm
Yes, that's what I mean.
Title: Re: Default event
Post by: howardpc on September 26, 2020, 03:32:56 pm
If your componenteditor descends from TDefaultComponentEditor then the default event assigned by double-clicking in the designer will be

OnCreate
OnChange
OnClick
in that order, depending on which event(s) are present.
You can override TComponentEditor.GetVerb to customise this further if the above does not suit.
Title: Re: Default event
Post by: pcurtis on September 26, 2020, 04:37:12 pm
My component descends from TCustomControl.
Title: Re: Default event
Post by: wp on September 26, 2020, 04:48:01 pm
As already noted by Howard, the default event is defined by the ComponentEditor of the component. This field of Lazarus is not very well documented. But there are lots of examples in the official Lazarus sources: go to folder components/ideintf and open unit componenteditors.pas -- it implements the default component editor as well as some special component editors.

Navigating to TDefaultComponentEditor I saw a property BestEditEvent. This seems to be the default event. So, the task is to redefine the BestEdit event, and this will change the event which is created on a double-click on a control.

I managed to alter the default event of a TNotebook from OnBoundsChanged to OnResize by doing these steps
Here is a copy of the simple unit, but the entire package code is also included in the attachment
Code: Pascal  [Select][+][-]
  1. unit TestUnit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, ComponentEditors;
  9.  
  10. type
  11.   TMyNotebookEditor = class(TUntabbedNotebookComponentEditor)
  12.   public
  13.     constructor Create(AComponent: TComponent;
  14.       ADesigner: TComponentEditorDesigner); override;
  15.   end;
  16.  
  17. procedure Register;
  18.  
  19. implementation
  20.  
  21. uses
  22.   ExtCtrls;
  23.  
  24. constructor TMyNotebookEditor.Create(AComponent: TComponent;
  25.   ADesigner: TComponentEditorDesigner);
  26. begin
  27.   inherited;
  28.   BestEditEvent := 'OnResize';
  29. end;
  30.  
  31. procedure Register;
  32. begin
  33.   RegisterComponentEditor(TNotebook, TMyNotebookEditor);
  34. end;
  35.  
  36. end.
TinyPortal © 2005-2018