Recent

Author Topic: Default event  (Read 1214 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Default event
« 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.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Default event
« Reply #1 on: September 26, 2020, 01:06:32 pm »
If your component descends from TControl, you can override its DblClick method.

furious programming

  • Hero Member
  • *****
  • Posts: 852
Re: Default event
« Reply #2 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.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Default event
« Reply #3 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?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Default event
« Reply #4 on: September 26, 2020, 03:17:38 pm »
Yes, that's what I mean.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Default event
« Reply #5 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.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Default event
« Reply #6 on: September 26, 2020, 04:37:12 pm »
My component descends from TCustomControl.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Default event
« Reply #7 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
  • Since I do not want to modify the LCL code with my example I create a new package and save it somewhere. Add package IDEIntf to the requirements in order to have access to all registration stuff and the default component editor. Of course, you can re-use the registration unit of your own package for this purpose.
  • Add a new unit (named TestUnit here, but a better name should be more appropriate; of course you can also use the component unit or registration unit of your component).
  • Add ComponentEditors to the interface "uses" and create a class which inherits from the ComponentEditor used by the component to be changed. In my case, this is TUntabbedNotebookComponentEditor, in other cases TDefaultComponentEditor is probably enough.
  • Since TUntabbedNotebookComponentEditor does not have its own constructor, add a Create constructor and set "BestEditEvent" to the name of the event which will become the new default event -- as simple as that! BestEditEvent is a string.
  • What's missing is registration of the new component editor (RegisterComponentEditor() ), and rebuilding the IDE
  • After the next start of the IDE, the TNotebook inserts an OnResize event upon a double-click
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.
« Last Edit: September 26, 2020, 04:56:59 pm by wp »

 

TinyPortal © 2005-2018