订阅在VB6 C#.NET事件事件、NET

2023-09-03 13:31:41 作者:我本流氓无限嚣张

我需要能够处理一个.NET事件在VB6。到目前为止,我有它设置,使我的C#类COM可见。我的VB6对象可以调用它的方法很好,但现在我需要一些方法,从.NET到VB进行沟通。如果我添加一个事件,我的C#类NET包装似乎添加add_EventName和remove_EventName我以为这是订阅和退订事件。但我还是一个新手,当涉及到VB6和到来,所以我真的不知道如何使用它。

I need to get be able to handle a .net event in VB6. So far i have it set up by making me c# class COM visible. My VB6 object can call methods on it fine but now i need some way to communicate from .net to VB. If i add an event to my c# class the .net wrapper seems to add an add_EventName and remove_EventName which i assume this is to subscribe and unsubscribe to the event. But i'm still a novice when it comes to VB6 and come so i'm not really sure how to use it.

在add_EventName似乎采取EventNameEventHadler,但我怎么给呢?我试图子,但这给了我一个运行时错误。任何人都知道如何使用它?这里是什么,我有一个例子

The add_EventName seems to take an EventNameEventHadler but what do i give it? i tried the sub but this gives me a runtime error. Anyone know how to use this? Here is an example of what i have

Private oHost As HostService.IHost

Private Sub Form_Load()
    Set oHost = New HostService.Host
    oHost.Start
    oHost.add_EvalReceived EvalReceivedEventHandler
End Sub

Private Sub EvalReceivedEventHandler(ByVal sender As Variant, ByVal e As EvalReceivedEventArgs)
MsgBox "Eval Received in VB: " & e.Eval.TimeSent & ":" & e.Eval.FirstName & " " & e.Eval.LastName & " - " & e.Eval.Comments
End Sub

所以oHost.add_EvalReceived行是错误的。

So oHost.add_EvalReceived line is wrong

推荐答案

您可以尝试添加 WithEvents就 oHost

Private WithEvents oHost As HostService.IHost

那么IDE应该让你创建 oHost 事件处理程序。这就像让你的的Form_Load 事件处理程序。在code窗口左上角的下拉应该让你选择 oHost

Then the IDE should allow you to create event handlers on oHost. It's just like making your Form_Load event handler. The drop-down at the top-left of the code window should let you select oHost.

免责声明:我用了很多次处理COM对象的事件。我从来没有真正试图通过互操作处理从.NET对象的事件,但我想你一定不喜欢这样。

Disclaimer: I've used this many times to handle events from COM objects. I've never actually tried handling events from a .Net object through interop, but I would think you must do it like this.