-
I am trying raise a single event but have multiple event handlers for that event.
I have a serial bus which I want to monitor continually (polling) when a packet arrives I want to raise an event that multiple forms can respond to. If the packet is for them they flag this up on the screen.
This is some psedo code of what I have:
Form:
Private withevents handlename as sourceclass
Private sub form_load()
Set handlename as sourceclass
handlename.canread ' start polling
end sub
Private sub handlename_extension ' event handler
label.caption="Packet for me"
endsub
Class: (event source) name=sourceclass
Public event extension()
private sub canread()
wait for packet..
raise event extension
doevents
loop
end sub
The above code works ok except every form has a instance of the class (event source) and I only want one event source running. I have tried varies ways of creating a class outside the form and then refering to it (public withevents) but I cant get it to work.
Any ideas?
Cheers
-
I just did a simple test....
Class module:
(Called Class1)
Event testje()
Public Sub foo()
RaiseEvent testje
End Sub
2 forms, form1 and form2
2 buttons on form1
this is the code in form1:
Public WithEvents test As Class1
Private Sub Command1_Click()
Form2.Show
End Sub
Private Sub Command2_Click()
test.foo
End Sub
Private Sub Form_Load()
Set test = New Class1
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set test = Nothing
End Sub
Private Sub test_testje()
MsgBox "test form1"
End Sub
this is the code in form2:
Private WithEvents test As Class1
Private Sub Form_Load()
Set test = Form1.test
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set test = Nothing
End Sub
Private Sub test_testje()
MsgBox "Test form 2"
End Sub
when i press Command2, I first get the msgbox in form1, and right after that, i get the msgbox in form2.
Hope this helps
-
Cheers Crazy D, that works a treat