The battle to get events to fire from C# to VB
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;namespace ComEventTest2
{
public delegate void TestEventHandler(); #region interface defining events
/// This is the interface for the Event
/// It will be used with ComSourceInterfaces for the class definition
[ComVisible(true)]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface IClass1_Event
{
void Test();
}
#endregion #region interface normal stuff
/// This is the interface for other methods etc which are to be exposed to the
/// consuming program
[ComVisible(true)]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface IClass1_Code
{
void DoSomething();
}
#endregion #region class /// This is the class
/// It implements IClass1_Code
/// It uses ComSourceInterfaces to specify which events are valid
/// It sets ClassInterface to be None to prevent any other methods from
/// being displayed in the consuming program
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[ComSourceInterfaces(typeof(IClass1_Event))]
public class Class1 : IClass1_Code
{
public Class1()
{
} public event TestEventHandler Test; public void DoSomething()
{
Test();
}
}
#endregion
}The code within VB is as follows:
'Define the object
Dim WithEvents test As ComEventTest2.Class1Private Sub Form_Load() 'initialise the objects
Set test = New ComEventTest2.Class1 'call the DoSomething method (this fires the event)
test.DoSomething End SubPrivate Sub test_Test() 'Let us know that the event was fired and captured ok
MsgBox "The event has fired"End Sub
Now to get it to work for real against VBA in an Access interface. Wish me luck!