Programming => General => Topic started by: pcurtis on October 30, 2020, 07:43:55 pm

Title: [SOLVED] Components [again]
Post by: pcurtis 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.
Title: Re: Components [again]
Post by: Handoko 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;
  6. procedure TMyButton.SetAddClick(anEvent: TNotifyEvent);
  7. begin
  8.   fbtnADD.OnClick := anEvent;
  9. end;
  11. // Add the similar setter and getter for Save and Cancel
  12. // ...
Title: Re: Components [again]
Post by: pcurtis on October 30, 2020, 08:30:02 pm
Perfect. Thanks.

Is there a good tutorial on this subject?
Title: Re: [SOLVED] Components [again]
Post by: howardpc 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).
Title: Re: [SOLVED] Components [again]
Post by: Sieben on October 30, 2020, 10:31:27 pm
And studying code... Here's another approach that might be handy if you add more buttons:
TinyPortal © 2005-2018