Page 1 of 1

Catch events on several forms

Posted: Friday 11th February 2022 12:34pm
by AMUR-WOLF
Is it possible to raise an event on one form and catch it on several other forms?

Re: Catch events on several forms

Posted: Friday 11th February 2022 3:13pm
by cogier
Try the attached code. What are you trying to achieve?
NewTest-0.0.1.tar.gz
(78.72 KiB) Downloaded 175 times

Re: Catch events on several forms

Posted: Friday 11th February 2022 4:35pm
by AMUR-WOLF
I want to check if there is a built-in Event-Driven programming capability in Gambas. When some object raises an Event, the event flies in Air. If some other object is subscribed to this event, it catches event. Other objects will ignore this event. If many objects are subscribed to event, each of them catches it.
EventToAir-0.0.1.tar.gz
(12.5 KiB) Downloaded 162 times

Re: Catch events on several forms

Posted: Friday 11th February 2022 5:16pm
by BruceSteers
Take a look at Observer.class
http://gambaswiki.org/wiki/comp/gb/observer
http://gambaswiki.org/wiki/comp/gb/observer/_new

Each form you want to catch an event of an object you can do something this...


Private hObs As Observer

Public Sub Form_Open()

  hObs = New Observer(TheClass.TheObjectToMonitor) As "ObjectName"

End

Public Sub ObjectName_EventName()

 ' Do something

End


Re: Catch events on several forms

Posted: Friday 11th February 2022 6:23pm
by BruceSteers
BruceSteers wrote: Friday 11th February 2022 5:16pm Take a look at Observer.class
http://gambaswiki.org/wiki/comp/gb/observer
http://gambaswiki.org/wiki/comp/gb/observer/_new

Attached is a simple example.
on opening it opens 3 forms
all are set to monitor the test button of form1 , the Click event is reported in each forms label.

Notes.
Form1.btnTest had to have it's "Public" property set to True so it can be accessed by the other forms.

Re: Catch events on several forms

Posted: Saturday 12th February 2022 12:28pm
by BruceSteers
Sorry i was a little low on time yesterday but had more time today, I have edited your posted project to use Observer.class

Notes..
Using Object.Attach will "Detach" the object from where it is attached to and has to be re-attached, Observer will intercept and only not trigger the original handler if using "Stop Event"

The Observer did not work on the main class, i had to make FMain.IslandFormObject a public object then use that on the Observers.

New Observer() takes a second argument "After" as Boolean to control if you want your observer to trigger before or after the main objects event is triggered.

Re: Catch events on several forms

Posted: Saturday 12th February 2022 11:41pm
by AMUR-WOLF
BruceSteers wrote: Saturday 12th February 2022 12:28pm Sorry i was a little low on time yesterday but had more time today, I have edited your posted project to use Observer.class
...
Thanks for the help!

Re: Catch events on several forms

Posted: Sunday 13th February 2022 6:24pm
by AMUR-WOLF
Three forms with labels observed the button on fourth one. This application was strongly coupled, that was not good. I tried to decouple forms with labels and button using the publisher-observer design pattern. Now the three forms observe a neutral singleton "Topic" and know nothing about objects that actually raise events. Now I can add a second form with button without any impact on the forms-observers. Now there could be null, one or many travellers that raise SOS event - no impact on null, one or many expectants for SOS event.