Catch events on several forms

Post

Posted
Rating:
#1 (In Topic #818)
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’
Is it possible to raise an event on one form and catch it on several other forms?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
Try the attached code. What are you trying to achieve?

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’
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.
Attachment
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
Take a look at Observer.class
/comp/gb/observer - Gambas Documentation
/comp/gb/observer/_new - Gambas Documentation

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


Code (gambas)

  1.  
  2.  
  3. Public Sub Form_Open()
  4.  
  5.   hObs = New Observer(TheClass.TheObjectToMonitor) As "ObjectName"
  6.  
  7.  
  8. Public Sub ObjectName_EventName()
  9.  
  10.  ' Do something
  11.  
  12.  
  13.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’

BruceSteers said



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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’

BruceSteers said

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!
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Regular
AMUR-WOLF is in the usergroup ‘Regular’
 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.

Attachment
Online now: No Back to the top
1 guest and 0 members have just viewed this.