【发布时间】:2011-02-15 12:52:33
【问题描述】:
在 VB6 中通过 COM 处理在 .NET 类上公开的事件
我的测试 .NET(在编译器设置中为互操作注册的类库)代码:
Imports System.Runtime.InteropServices
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch), ComVisible(True)> _
Public Interface MyEventInterface
<DispId(1)> Event Exploded(ByVal Text As String)
<DispId(2)> Sub PushRedButton()
End Interface
<ClassInterface(ClassInterfaceType.None)> _
Public Class EventTest
Implements MyEventInterface
Public Event Exploded(ByVal Text As String) Implements MyEventInterface.Exploded
Public Sub PushRedButton() Implements MyEventInterface.PushRedButton
RaiseEvent Exploded("Bang")
End Sub
End Class
我的测试VB6应用程序winforms代码(引用了上面的类库):
Public ct As New ComTest1.EventTest
Private Sub Command1_Click()
ct.add_Exploded (ExplodedHandler)
ct.PushRedButton
ct.remove_Exploded (ExplodedHandler)
End Sub
Private Sub ExplodedHandler(ByVal Text As String)
MsgBox Text
End Sub
特别是我不确定如何在 VB6 中设置处理程序,我得到的编译错误是 VB6 中这一行的“Argument not optional”:
ct.add_Exploded (ExplodedHandler)
【问题讨论】:
-
你的 add_Exploded 声明在哪里?
-
COM 属性创建了它。即在 VB6 中智能感知 add_ 和 remove_ 由于我相信的 Idispatch 接口而存在?