Subscribe to C# .net Event in VB6

こ雲淡風輕ζ 提交于 2019-12-03 15:58:15

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.

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);
}

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

oHost.add_EvalReceived AddressOf EvalReceivedEventHandler
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!