Recent

Author Topic: Event Listeners in Lazarus - free library  (Read 2142 times)

cdbc

  • Hero Member
  • *****
  • Posts: 1588
    • http://www.cdbc.dk
Re: Event Listeners in Lazarus - free library
« Reply #15 on: October 03, 2024, 01:49:24 pm »
Hi
Quote
Let's hope that one of the Free Pascal experts will someday have free time and a desire
to show a simple example of how to declare and use an observer.
I'm by no means an expert, but...
There's an example here of using the built-in observer-pattern(TPersistent).
And on page 2 of this thread, there are some examples of using it as well.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

LBox

  • New Member
  • *
  • Posts: 33
Re: Event Listeners in Lazarus - free library
« Reply #16 on: October 03, 2024, 05:43:12 pm »
I noticed a reference to wanting tutorials and YouTube channel. Let me know what you are looking for and I can create a video for it next :)

Wow that would be really cool  8) 👍

For example, you can show the interaction between TEdit and TLabel, where TEdit will be observed, and TLabel will be the observer,
and every time TEdit.Text is changed, TLabel will react to this and change its TLabel.Caption value to TEdit.Text value  :)

Of course, this can be done via the TEdit change event, but most beginners like me already know and understand this method
because 99% of Free Pascal lessons show the logic of starting a procedure via basic standard events.

By the way, thinking about the observer method, I came up with a trick that does not require an observer or an explicit start of a cycle
in the procedure in order to find the desired object to apply changes to it from the UI controller  ::)

The essence of the trick is that an object that wants to bind its parameters to some Spin controller, when clicked on, transfers its name to the "capture" parameter of the TPanel,
which is the parent of this Spin controller, and when this Spin controller triggers a change event, it can find the target object by name that is written to the "capture" parameter
of the parent Panel and apply any possible actions to it :)

Here is a very simple working example :D

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SpinEdit1Change(Sender: TObject);
  2. var
  3.   TargetObjName: String;
  4.   TargetObj: TControl;
  5. begin
  6.   // Get the name of the target object
  7.   TargetObjName:= TSpinEdit(Sender).Parent.Caption; // Here the target object transfers its name when the user selects it
  8.   TargetObj:= FindChildControl(TargetObjName); // Find the target object by name
  9.   TargetObj.Left:= TSpinEdit(Sender).Value; // Apply the change to the target object
  10. end;
  11.  
« Last Edit: October 03, 2024, 05:56:36 pm by LBox »

LBox

  • New Member
  • *
  • Posts: 33
Re: Event Listeners in Lazarus - free library
« Reply #17 on: October 03, 2024, 05:51:50 pm »
Hi
Quote
Let's hope that one of the Free Pascal experts will someday have free time and a desire
to show a simple example of how to declare and use an observer.
I'm by no means an expert, but...
There's an example here of using the built-in observer-pattern(TPersistent).
And on page 2 of this thread, there are some examples of using it as well.
Regards Benny

Thank you very much, I will definitely take a look  :)

Thaddy

  • Hero Member
  • *****
  • Posts: 16018
  • Censorship about opinions does not belong here.
Re: Event Listeners in Lazarus - free library
« Reply #18 on: October 03, 2024, 08:20:05 pm »
There at least 10 examples available by just searching the forum.
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018