2010-05-04 207 views
2

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

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

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代码(引用上面的类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我不知道如何设置处理程序在VB6编译错误我得到的是“参数不选择有理”在这条线在VB6:

ct.add_Exploded (ExplodedHandler) 
+0

哪里是你的add_Exploded声明? – volody 2010-05-04 16:04:52

+0

COM属性创建它。即在VB6中,由于我相信Idispatch接口,intellisense add_和remove_是否存在? – PeanutPower 2010-05-05 08:56:58

回答

2

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

.NET类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测试应用:

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 
+0

有没有人知道是否有可能做到这一点只有一个界面的事件和属性/方法? – PeanutPower 2010-05-05 11:05:37

4

C#

[ComVisible(true)] //Exposed 
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] //Our managed interface will be IDispatch 
public interface IMathEvents 
{ 

    void OnOperationCompleted(string message); 

} 
[ComVisible(true)] 
[ClassInterface(ClassInterfaceType.None)] 
[ComSourceInterfaces(typeof(IMathEvents))] //Our event source is IMathEvents interface 
[ComDefaultInterface(typeof(IMath))] 
public class Math:IMath /*IMath interface just declares a method named Add */ 
{ 
    public int Add(int x, int y) 
    { 
     if (null != this.OnOperationCompleted) 
      this.OnOperationCompleted("Operation completed, result: " + (x+y).ToString()); 
     return x + y; 
    } 

    [ComVisible(false)] 
    public delegate void OperationCompletedDel(string message); //No need to expose this delegate 
    public event OperationCompletedDel OnOperationCompleted; 

} 
[ComVisible(true)] 
public interface IMath 
{ 
    int Add(int x, int y); 
} 

VB

私人WITHEVENTS米作为数学

私人小组的Form_Load()

Set m = New PayClient.Math 

结束子

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

MsgBox Text 

末次

私人小组Command2_Click()
m.Add 1,2

末次