问题
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:
- Main VB6 program starts
- VB6 creates a (C#)
ModalCSForm
object and sets its a reference to aVB6Bridge
object - VB6 makes the call to show
ModalCSForm
ModalCSForm
is shown- User clicks button on
ModalCSForm
- Button click handler uses
VB6Bridge
object to showModalVB6Form
ModalVB6Form
is shown- 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