处理通过COM在VB6暴露在.NET类的事件 [英] Handling events exposed on a .NET class via COM in VB6

查看:139
本文介绍了处理通过COM在VB6暴露在.NET类的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过COM在VB6暴露在.NET类处理事件

我的测试.NET(类libary在编译器设置注册的互操作)code:

 导入了System.Runtime.InteropServices

< InterfaceType(ComInterfaceType.InterfaceIsIDispatch),标记有ComVisible特性(真)> _
公共接口MyEventInterface
    < D​​ISPID(1)>事件引爆(BYVAL文本作为字符串)
    < D​​ISPID(2)>子PushRedButton()
结束接口


< ClassInterface(ClassInterfaceType.None)> _
公共类EventTest
    实现MyEventInterface

    公共事件引爆(BYVAL文本作为字符串)实现MyEventInterface.Exploded

    公用Sub PushRedButton()实现MyEventInterface.PushRedButton

        爆炸的RaiseEvent(砰)

    结束小组

末级
 

我的测试VB6应用程序的WinForms code(引用上面的类libary):

 公共克拉作为新ComTest1.EventTest

私人小组Command1_Click()

    ct.add_Exploded(ExplodedHandler)

    ct.PushRedButton

    ct.remove_Exploded(ExplodedHandler)

结束小组

私人小组ExplodedHandler(BYVAL文本作为字符串)

    MSGBOX文本

结束小组
 

Specifially我不知道如何设置处理程序在VB6编译错误我得到的是参数不可选在这条线在VB6:

  ct.add_Exploded(ExplodedHandler)
 

解决方案

使用< ComSourceInterfaces(的GetType(接口名称))> 在.NET中的事件和 WithEvents就在VB6

.NET类libary:

 导入了System.Runtime.InteropServices

这个接口​​是揭露事件
<的Guid(28C7DCE1-90EF-4a30-AF7F-4187F9FFFDEB)> _
< InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
公共接口MathsEvents
    < D​​ISPID(1)> _
    分计算(BYVAL结果作为双)
结束接口

这个接口​​是公开的属性和方法
<的Guid(86CE5E8D-777D-4cd5-8A7D-7F58737F1DB4)> _
< InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
公共接口_Maths
    < D​​ISPID(2)> _
    属性值a()作为双
    < D​​ISPID(3)> _
    物业valueB,则()作为双
    < D​​ISPID(4)> _
    只读属性结果()作为双
    < D​​ISPID(5)> _
    子的Add()
结束接口

这是实际的类
'的事件,通过使用ComSourceInterfaces属性暴露
的属性和方法使用Implements关键字暴露
<的Guid(C58721B1-15B3-4eeb-9E1E-BCDA33D38EE6)> _
< ClassInterface(ClassInterfaceType.None)> _
< ComSourceInterfaces(的GetType(MathsEvents))> _
公共类数学
    器具_Maths
    公共事件计算(BYVAL结果作为双)
    私人mValueA作为双
    私人mValueB作为双
    私人mResult作为双

    公共属性值a()作为双器具_Maths.ValueA
        得到
            返回mValueA
        最终获取
        设置(BYVAL值作为双)
            mValueA =价值
        结束设定
    高端物业

    公共财产valueB,则()作为双器具_Maths.ValueB
        得到
            返回mValueB
        最终获取
        设置(BYVAL值作为双)
            mValueB =价值
        结束设定
    高端物业

    公共只读属性结果()作为双器具_Maths.Result
        得到
            返回mResult
        最终获取

    高端物业

    公共子新()
        mValueA = 0
        mValueB = 0
        mResult = 0
    结束小组

    公用Sub添加()实现_Maths.Add
        mResult = mValueA + mValueB
        计算的RaiseEvent(mResult)
    结束小组

末级
 

VB6测试应用程序:

 私人WithEvents就计算作为数学

私人小组btnAdd_Click()

   calc.ValueA = CDbl(txtValueA.Text)
   calc.ValueB = CDbl(txtValueB.Text)
   calc.Add

结束小组

私人小组calc_Calculated(BYVAL结果作为双)
   txtResult.Text = CStr的(结果)
结束小组

私人小组的Form_Load()

   将计算值=数学新

结束小组
 

Handling events exposed on a .NET class via COM in VB6

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

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

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)

解决方案

Use <ComSourceInterfaces(GetType(the interface name))> for the events in .NET and WithEvents in VB6

.NET class libary:

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 test application:

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

这篇关于处理通过COM在VB6暴露在.NET类的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆