Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

com - Subscribe to C# .net Event in 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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...