讨论个砖家级问题:两个类之间如何发送接收事件

dafu2003 2011-01-21 11:09:55
两个类:class1 class2,
现在想要在他们之间收发自定义事件,比如在class1中 raiseEvents processMessage(msgID, msg as string),
在class2中如何接受?
...全文
150 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
king06 2011-01-21
  • 打赏
  • 举报
回复
哦,上面6F自己说明了...
king06 2011-01-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 spt_petrolor 的回复:]
好像是不能在类中接受处理事件。
在类中定义对class1的对象时 public withevents mm as class1会出错
[/Quote]
可以定义啊
form1:
Private Sub Command1_Click()
Dim cl2 As New Class2
Call cl2.test2
End Sub

class1:
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
王二.麻子 2011-01-21
  • 打赏
  • 举报
回复
说错了,好久没VB了[Quote=引用 2 楼 spt_petrolor 的回复:]
好像是不能在类中接受处理事件。
在类中定义对class1的对象时 public withevents mm as class1会出错
[/Quote]
在class1里面不能创建class2的实例。但是可以对其他地方创建的class2的实例的引用,用这个引用可以处理class1的事件
以前写过一个这样的程序。
在class2里面:public withevents mm as class1
然后写mm的事件处理过程。
在form1里面创建class1实例cls1,class2实例cls2,并且set cls2.mm=cls1
不知道为什么这么麻烦。

类里面,withevents不能和new一起,set mm=new **也老出错误。
jiashie 2011-01-21
  • 打赏
  • 举报
回复
clsA内部引用clsB
private mListener as clsB

clsB内部引用clsA
private mListener as clsA

然后在raiseevents时,直接调用mListener.OnMyEvent(...)

'// 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



纯新手,高手表笑
咸清 2011-01-21
  • 打赏
  • 举报
回复
高手现在都过年去了?
~~这样的帖子得顶啊
王二.麻子 2011-01-21
  • 打赏
  • 举报
回复
好像是不能在类中接受处理事件。
在类中定义对class1的对象时 public withevents mm as class1会出错
咸清 2011-01-21
  • 打赏
  • 举报
回复
方法很多,比如说 DDE
lyserver 2011-01-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 dafu2003 的回复:]
两个类:class1 class2,
现在想要在他们之间收发自定义事件,比如在class1中 raiseEvents processMessage(msgID, msg as string),
在class2中如何接受?
[/Quote]
用WithEvents就行了。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧