Here at work we have an old VB6 application I need to teach new(er) tricks. The first thing I had to do was have it call methods from a .Net COM-visible DLL written in C#. I have that working. Now I need to have it handle incoming progress notification events from that same DLL. Here is the C# code:
namespace NewTricksDLL
{
[ComVisible(true)]
[ComSourceInterfaces(typeof(IManagedEventsToCOM))]
public class NewTricks
{
public delegate void NotificationEventHandler(string Message);
public event NotificationEventHandler NotifyEvent = null;
public string SomeMethod(string message)
{
return Notify(message);
}
private string Notify(string message)
{
if (NotifyEvent != null)
NotifyEvent("Notification Event Raised.");
return message;
}
}
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
interface IManagedEventsToCOM
{
[DispId(1)]
void NotifyEvent(string Message);
}
}
And here is how I attempt to use it in a VB6 form
Option Explicit
Public WithEvents newTricks as NewTricksDLL.NewTricks
Private Sub Command1_Click()
Dim response as String
Set newTricks = New NewTricksDLL.NewTricks
AddHandler newTricks.NotifyEvent, AddressOf NotifyEventHandler
response = newTricks.SomeMethod("Please send an event...")
End Sub
Private Sub NotifyEventHandler()
'Nothing
End Sub
My problem is that when I attempt to run the VB6 form I get Compile error: Object does not source automation events.
If I remove WithEvents
the Command1_Click
Sub does run and response
does contain "Please send an event..."
so I know the method is being called via COM.
Where am I going wrong with the implementation of the event?
AddHandler
is a vb.net verb, not vb6. If you try to compile this code with vb6, you should receive a compile error "Sub or Function not defined". Unless you have a Sub or Function named AddHandler? – MarkL