我想使用下面的代码在VB6中引发一个事件。C#/ VB6与表单互操作
public delegate void EventHandler();
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ISOMEINTERFACE
{
[DispId(1)]
void MyEvent();
}
[ClassInterface(ClassInterfaceType.AutoDual),
ComSourceInterfaces(typeof(ISOMEINTERFACE)),
ComVisible(true)]
public class clsConVB6
{
public event EventHandler MyEvent;
public void DoSomething()
{
// both events are, the one from this class and the one from Form1, reach this void
MessageBox.Show("Event");
MyEvent(); //does not raise an event when called from an event from Form1
}
public void ComInteropTest()
{
//-> this event is working fine
DoSomething();
Form1 frm = new Form1();
frm.myFormEvent += new Form1.SomeEventHandler(DoSomething);
frm.ShowDialog();
}
}
代码在Form1中:
public delegate void SomeEventHandler();
public event SomeEventHandler myFormEvent;
private void button1_Click(object sender, EventArgs e)
{
//raises an event in the c#-class clsConVB6, not in VB6!
myFormEvent();
}
在我的VB6级我收到的第一个事件,但我没有收到第二个,由窗体上的按钮,点击创建。 Form1中的事件将事件发送到clsConVB6类。这工作正常,因为我总是得到“event-msgbox”。最后,clsConVB6应该在VB6中引发事件,但我没有收到事件。
一旦涉及表单,为什么它不起作用?非常感谢您的支持。
这很明显。您在clsConVB6上暴露了一个事件,但在Form1上未暴露。您的VB6代码无法订阅不可见类上的事件。 – 2012-03-30 13:18:50