3

我正在为已经部署的 VB6 可执行文件构建一个“插件”。我正在使用 .NET 和 COM-Interop。VB6 创建一个空白表单,然后将我的 .NET UserControl 加载到其中(但现在 .dll 已编译为 VB6 可以看到的 .ocx ActiveX UserControl)。

我让它运行良好,但我希望能够从我的 .NET 代码内部关闭 VB6 父窗体。我可以将 VB6 代码添加到我的 VB6 化 UserControl 中,但我似乎找不到在 UserControl 被销毁时触发的事件。

到目前为止我已经尝试过:

  • 从我的 .NET 控件的 Disposing 事件中调用ParentForm.Close。接收错误Object Reference not set to Instance of an Object.
  • 试图从 VB6 关闭(我可以从那里获得父表单的句柄)。使用ControlRemoved,Terminated和其他一些回想起来确实没有意义的骇人听闻的变通办法不会被触发。
  • 调用Application.Exit(此时真的变得绝望)关闭了整个应用程序(谁会重击......)

我查看了放入 .NET 控件的 VB6 互操作代码,以下内容看起来很有希望:

#Region "VB6 Events"

'This section shows some examples of exposing a UserControl's events to VB6.  Typically, you just
'1) Declare the event as you want it to be shown in VB6
'2) Raise the event in the appropriate UserControl event.

Public Shadows Event Click() 'Event must be marked as Shadows since .NET UserControls have the same name.
Public Event DblClick()

Private Sub InteropUserControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
    RaiseEvent Click()
End Sub

Private Sub InteropUserControl_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
    RaiseEvent DblClick()
End Sub

#End Region

只是在本节中添加一个事件吗?我对 Interop 或 VB6 不是很熟悉。

4

1 回答 1

2

Alright, I figured it out and will post what I did for future generations :P

I was right with the event handlers in the VB6 code, and MarkJ was correct as well.

I created an event in the .NET code,

Public Event KillApp()

and then when I wanted to close everything, raised it:

RaiseEvent KillApp()

In the VB6 UserControl code, I declared the event again,

Public Event KillApp()

and then added a handler for it:

Private Sub MenuCtl_KillApp()
    Unload Me.ParentForm
End Sub

where MenuCtl is my instance of the .NET control, and Me.ParentForm is the VB6 container form that houses the control. It now correctly closes the form!

In retrospect it makes a lot of sense, but I was unaware that you could pass events back and forth between managed/unmanaged that easily.

于 2013-06-14T15:43:23.073 回答