Catch events on several forms

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 6th February 2022 8:41am
Location: Russia

Catch events on several forms

Post by AMUR-WOLF »

Is it possible to raise an event on one form and catch it on several other forms?
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Catch events on several forms

Post by cogier »

Try the attached code. What are you trying to achieve?
NewTest-0.0.1.tar.gz
(78.72 KiB) Downloaded 162 times
User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 6th February 2022 8:41am
Location: Russia

Re: Catch events on several forms

Post 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 150 times
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Catch events on several forms

Post 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

If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Catch events on several forms

Post 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.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Catch events on several forms

Post 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.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 6th February 2022 8:41am
Location: Russia

Re: Catch events on several forms

Post 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!
User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 6th February 2022 8:41am
Location: Russia

Re: Catch events on several forms

Post 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.
Attachments
EventToAir-0.0.3.tar.gz
(12.88 KiB) Downloaded 158 times
Post Reply