Recent

Author Topic: [SOLVED] Components [again]  (Read 692 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Components [again]
« on: October 30, 2020, 07:43:55 pm »
Hi All,

I know I've asked this before but I can't figure out how to change it.

In the attachment is a simple component. I just want to add a seperate OnClick event for each button.

Any help is greatly appreciated.
« Last Edit: October 30, 2020, 08:30:47 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Components [again]
« Reply #1 on: October 30, 2020, 07:58:20 pm »
You should add the properties for AddClick, SaveClick and CancelClick:

Code: Pascal  [Select][+][-]
  1.   TMyButton = class(TCustomControl)
  2.   private
  3.     fbtnADD : TBitBtn;
  4.     fbtnSAVE : TBitBtn;
  5.     fbtnCANCEL : TBitBtn;
  6.     // ...
  7.     function GetAddClick: TNotifyEvent;
  8.     procedure SetAddClick(anEvent: TNotifyEvent);
  9.     function GetSaveClick: TNotifyEvent;
  10.     procedure SetSaveClick(anEvent: TNotifyEvent);
  11.     function GetCancelClick: TNotifyEvent;
  12.     procedure SetCancelClick(anEvent: TNotifyEvent);
  13.     //...
  14.   published
  15.     property AddClick: TNotifyEvent read GetAddClick write SetAddClick;
  16.     property SaveClick: TNotifyEvent read GetSaveClick write SetSaveClick;
  17.     property CancelClick: TNotifyEvent read GetCancelClick write SetCancelClick;
  18.   end;

And then the code for setters and getters. The setters and getters are something like this:

Code: Pascal  [Select][+][-]
  1. function TMyButton.GetAddClick: TNotifyEvent;
  2. begin
  3.   Result := fbtnADD.OnClick;
  4. end;
  5.  
  6. procedure TMyButton.SetAddClick(anEvent: TNotifyEvent);
  7. begin
  8.   fbtnADD.OnClick := anEvent;
  9. end;
  10.  
  11. // Add the similar setter and getter for Save and Cancel
  12. // ...

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Components [again]
« Reply #2 on: October 30, 2020, 08:30:02 pm »
Perfect. Thanks.

Is there a good tutorial on this subject?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] Components [again]
« Reply #3 on: October 30, 2020, 08:58:08 pm »
In addition to the many online resources available (see the wiki and links) the Lazarus Handbook (see advert on the forum sidebar) has a good chapter about writing components, contributed by wp (it is chapter 11, in Volume 2).

Sieben

  • Sr. Member
  • ****
  • Posts: 310
Re: [SOLVED] Components [again]
« Reply #4 on: October 30, 2020, 10:31:27 pm »
And studying code... Here's another approach that might be handy if you add more buttons:
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

 

TinyPortal © 2005-2018