简体   繁体   中英

C# / VB6 interop with forms

I want to raise an event in VB6 using the code below.

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

code in 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();
}

In my VB6-class I receive the first event, but I do not receive the second one, that is created by a button click on a form. The event in Form1 sends an event to the clsConVB6-class. Thats working fine, because I always get the "event-msgbox". Finally, the clsConVB6 should raise an event in VB6, but I do not receive an event.

Why does it not work, once a form is involved? Many thanks for every support.

The suggestion given above was not the solution. I think, I was misunterstood, because the event in Form1 should not raise an event in VB 6, but in the C#-class clsConVB6. And there, the required event should be raised.

However, I solved the problem by creating an exe-file. The events have worked, BUT JUST IN THE CREATED EXE-FILE. At runtime, I do not receive any events in Visual Basic, but the events are working fine in the exe-file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM