Events do not fire when displaying a VB6 modal form from a C# modal form?

戏子无情 提交于 2020-02-23 10:13:00

问题


EDIT - I discovered that when the VB6 program runs compiled, the events work normally and there is no problem.

It is only when the code is running in the VB6 IDE that the events are broken!

This does not entirely solve the problem because it would be very inefficient development if we can't debug in the IDE; but maybe it provides more insight into the root cause. I guess the fix that is needed (if possible) is to get the events to work normally / correctly in the IDE.


I am loading a modal VB6 form from a modal C# Winform, using COM interop between the two environments. In this configuration (modal-to-modal) no events will fire in the VB6 form - e.g,. not even Form_Load.

If the forms are loaded non-modally than things do work normally, but of course this does not meet the UI requirements very well.

I would like to understand, if possible, the root cause of this issue and what if anything can be done about it. I have included here the most basic reproduction of the problem I could come up with.

The architecture is that a coordinating object is passed from VB6 to C# code which acts as wrapper to show the VB6 form.

Sequence of events:

  1. Main VB6 program starts
  2. VB6 creates a (C#) ModalCSForm object and sets its a reference to a VB6Bridge object
  3. VB6 makes the call to show ModalCSForm
  4. ModalCSForm is shown
  5. User clicks button on ModalCSForm
  6. Button click handler uses VB6Bridge object to show ModalVB6Form
  7. ModalVB6Form is shown
  8. No events occur in VB6. No breakpoints will be hit in VB6 code.

C# Project:

Class / form ModalCSForm:

[ClassInterface(ClassInterfaceType.AutoDual)]
public partial class ModalCSForm : Form
{
    ...

    public IVB6Bridge Bridge { get; set; }

    private void button1_Click(object sender, EventArgs e) =>
        Bridge.ShowVB6FormModally();
}

Interface IVB6Bridge:

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IVB6Bridge
{
    void ShowVB6FormModally();
}

VB6 Project:

Main program:

Sub main()
    Dim m As New ModalCSForm

    Set m.Bridge = New VB6Bridge
    Call m.ShowDialog
End Sub

ModalVB6Form code:

Private Sub Form_Load()
    Label1.Caption = ""  'Doesn't occur, remains "ERASE ME"
End Sub

Class VB6Bridge:

Implements IVB6Bridge

Private Sub IVB6Bridge_ShowVB6FormModally()
    ModalVB6Form.Show FormShowConstants.vbModal
End Sub

I've included a screenshot below to demonstrate the problem; obviously the text "ERASE ME" has in fact not been erased, which ought to be the first thing to occur upon form load. Similarly, breakpoints on Form_Load, Form_Initialize, or anything else in the VB6 form code will NOT be hit.

来源:https://stackoverflow.com/questions/57101436/events-do-not-fire-when-displaying-a-vb6-modal-form-from-a-c-sharp-modal-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!