Handling events exposed on a .NET class via COM in VB6(在 VB6 中通过 COM 处理在 .NET 类上公开的事件)
问题描述
在 VB6 中通过 COM 处理在 .NET 类上公开的事件
Handling events exposed on a .NET class via COM in VB6
我的测试 .NET(在编译器设置中为互操作注册的类库)代码:
My test .NET (class libary registered for interop in compiler settings) code:
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代码(引用了上面的类库):
My test VB6 application winforms code (which references the above class libary):
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":
Specifially I'm not sure how to set up the handler in VB6 the compile error I get is "Argument not optional" on this line in the VB6:
ct.add_Exploded (ExplodedHandler)
推荐答案
使用<ComSourceInterfaces(GetType(接口名称))> 中的事件.NET 和 VB6 中的 WithEvents
Use <ComSourceInterfaces(GetType(the interface name))> for the events in .NET and WithEvents in VB6
.NET 类库:
Imports System.Runtime.InteropServices
' This interface is to expose the events
<Guid("28C7DCE1-90EF-4a30-AF7F-4187F9FFFDEB")> _
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface MathsEvents
<DispId(1)> _
Sub Calculated(ByVal Result As Double)
End Interface
' This interface is to expose the properties and methods
<Guid("86CE5E8D-777D-4cd5-8A7D-7F58737F1DB4")> _
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface _Maths
<DispId(2)> _
Property ValueA() As Double
<DispId(3)> _
Property ValueB() As Double
<DispId(4)> _
ReadOnly Property Result() As Double
<DispId(5)> _
Sub Add()
End Interface
' This is the actual class
' The events are exposed by using the ComSourceInterfaces attribute
' The properties and methods are exposed using the Implements keyword
<Guid("C58721B1-15B3-4eeb-9E1E-BCDA33D38EE6")> _
<ClassInterface(ClassInterfaceType.None)> _
<ComSourceInterfaces(GetType(MathsEvents))> _
Public Class Maths
Implements _Maths
Public Event Calculated(ByVal Result As Double)
Private mValueA As Double
Private mValueB As Double
Private mResult As Double
Public Property ValueA() As Double Implements _Maths.ValueA
Get
Return mValueA
End Get
Set(ByVal value As Double)
mValueA = value
End Set
End Property
Public Property ValueB() As Double Implements _Maths.ValueB
Get
Return mValueB
End Get
Set(ByVal value As Double)
mValueB = value
End Set
End Property
Public ReadOnly Property Result() As Double Implements _Maths.Result
Get
Return mResult
End Get
End Property
Public Sub New()
mValueA = 0
mValueB = 0
mResult = 0
End Sub
Public Sub Add() Implements _Maths.Add
mResult = mValueA + mValueB
RaiseEvent Calculated(mResult)
End Sub
End Class
VB6 测试应用:
Private WithEvents calc As Maths
Private Sub btnAdd_Click()
calc.ValueA = CDbl(txtValueA.Text)
calc.ValueB = CDbl(txtValueB.Text)
calc.Add
End Sub
Private Sub calc_Calculated(ByVal Result As Double)
txtResult.Text = CStr(Result)
End Sub
Private Sub Form_Load()
Set calc = New Maths
End Sub
这篇关于在 VB6 中通过 COM 处理在 .NET 类上公开的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 VB6 中通过 COM 处理在 .NET 类上公开的事件
基础教程推荐
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
