7,763
社区成员
发帖
与我相关
我的任务
分享
Private Sub Command1_Click()
Dim cl2 As New Class2
Call cl2.test2
End Sub
Public Event processMessage(ByVal msgID As Long, ByRef msg As String)
Public Sub test1()
RaiseEvent processMessage(2, "nothing")
End Sub
class2:Private WithEvents mcl1 As Class1
Private Sub mcl1_processMessage(ByVal msgID As Long, msg As String)
MsgBox msgID & ":" & msg
End Sub
Public Sub test2()
Set mcl1 = New Class1
Call mcl1.test1
End Sub
'// IEventListener
Option Explicit
'Public Event MyEvent(ByVal a As String)
Public Sub OnMyEvent(ByVal a As String)
End Sub
'// clsA
Option Explicit
Implements IEventListener
'Public Event MyEvent(ByVal a As String)
Private mListener As IEventListener
Private Sub IEventListener_OnMyEvent(ByVal a As String)
Debug.Print "handled in clsA," & a
End Sub
Public Function AddEventListener(ByRef objListener As IEventListener)
Set mListener = objListener
End Function
Public Function SetValue(ByVal a As String)
'RaiseEvent MyEvent(a)
If Not (mListener Is Nothing) Then
mListener.OnMyEvent (a)
End If
End Function
'// clsB
Option Explicit
Implements IEventListener
'Public Event MyEvent(ByVal a As String)
Private mListener As IEventListener
Private Sub IEventListener_OnMyEvent(ByVal a As String)
Debug.Print "handled in clsB," & a
End Sub
Public Function AddEventListener(ByRef objListener As IEventListener)
Set mListener = objListener
End Function
Public Function SetValue(ByVal a As String)
'RaiseEvent MyEvent(a)
If Not (mListener Is Nothing) Then
mListener.OnMyEvent (a)
End If
End Function
'调用示例
Dim a As New clsA
Dim b As New clsB
a.AddEventListener b
b.AddEventListener a
a.SetValue (20)
b.SetValue (30)
Set a = Nothing
Set b = Nothing