订阅 VB6 中的 C# .net 事件

发布于 2024-11-29 12:31:48 字数 815 浏览 0 评论 0原文

我需要能够在 VB6 中处理 .net 事件。到目前为止,我已经通过使 c# 类 COM 可见来设置它。我的 VB6 对象可以很好地调用它的方法,但现在我需要某种方法从 .net 到 VB 进行通信。如果我向我的 c# 类添加一个事件,.net 包装器似乎会添加一个 add_EventName 和 remove_EventName,我认为这是订阅和取消订阅该事件。但对于VB6来说我还是个新手,所以我不太确定如何使用它。

add_EventName 似乎采用 EventNameEventHadler 但我该给它什么?我尝试了 sub,但这给了我一个运行时错误。有人知道如何使用这个吗?这是我所拥有的示例

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 行是错误的

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.

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

So oHost.add_EvalReceived line is wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小…红帽 2024-12-06 12:31:48

好的,我在 MarkJ 的帮助下成功了。我必须有一个代表 COM 事件的接口。所以他们最终看起来像这样

[ComSourceInterfaces(typeof(IHostEvents))]
[ClassInterface(ClassInterfaceType.None)]
[Guid("037CF765-4C30-4CF7-969C-1775E79844CE")]
public class Host : IHost
{
    //IHost implementation
}

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("8C0C3F0E-5793-4E11-AB20-3A556C0B6790")]
public interface IHostEvents
{
    [DispId(1)]
    void EvalReceived(object sender, EvalReceivedEventArgs e);
}

Ok i got it working with help from MarkJ. I had to have an interface that represents my events for COM. So they ended up looking like this

[ComSourceInterfaces(typeof(IHostEvents))]
[ClassInterface(ClassInterfaceType.None)]
[Guid("037CF765-4C30-4CF7-969C-1775E79844CE")]
public class Host : IHost
{
    //IHost implementation
}

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("8C0C3F0E-5793-4E11-AB20-3A556C0B6790")]
public interface IHostEvents
{
    [DispId(1)]
    void EvalReceived(object sender, EvalReceivedEventArgs e);
}
つ低調成傷 2024-12-06 12:31:48

您可以尝试将 WithEvents 添加到 oHost 声明中

Private WithEvents oHost As HostService.IHost

,然后 IDE 应允许您在 oHost 上创建事件处理程序。这就像创建 Form_Load 事件处理程序一样。代码窗口左上角的下拉列表应该让您选择oHost

免责声明:我已经多次使用它来处理来自 COM 对象的事件。我从未真正尝试过通过互操作处理来自 .Net 对象的事件,但我认为您必须这样做。

You could try adding WithEvents to your declaration of oHost

Private WithEvents oHost As HostService.IHost

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.

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.

瑕疵 2024-12-06 12:31:48

在 VB6 中,您可以使用 AddressOf 运算符隐式创建委托:

oHost.add_EvalReceived AddressOf EvalReceivedEventHandler

In VB6 you can use the AddressOf operator to create the delegate implicitly:

oHost.add_EvalReceived AddressOf EvalReceivedEventHandler
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文