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 06th February 2022 8:41am
Location: Russia

Catch events on several forms

Post by AMUR-WOLF » Friday 11th February 2022 12:34pm

Is it possible to raise an event on one form and catch it on several other forms?

User avatar
cogier
Site Admin
Posts: 875
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Catch events on several forms

Post by cogier » Friday 11th February 2022 3:13pm

Try the attached code. What are you trying to achieve?
NewTest-0.0.1.tar.gz
(78.72 KiB) Downloaded 51 times

User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 06th February 2022 8:41am
Location: Russia

Re: Catch events on several forms

Post by AMUR-WOLF » Friday 11th February 2022 4:35pm

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 49 times

BruceSteers
Posts: 766
Joined: Thursday 23rd July 2020 5:20pm

Re: Catch events on several forms

Post by BruceSteers » 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

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


BruceSteers
Posts: 766
Joined: Thursday 23rd July 2020 5:20pm

Re: Catch events on several forms

Post by BruceSteers » Friday 11th February 2022 6:23pm

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.

BruceSteers
Posts: 766
Joined: Thursday 23rd July 2020 5:20pm

Re: Catch events on several forms

Post by BruceSteers » 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

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.

User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 06th February 2022 8:41am
Location: Russia

Re: Catch events on several forms

Post by AMUR-WOLF » Saturday 12th February 2022 11:41pm

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 06th February 2022 8:41am
Location: Russia

Re: Catch events on several forms

Post by AMUR-WOLF » Sunday 13th February 2022 6:24pm

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 53 times

Post Reply