1

我的场景:

具有基本主 (mdi) 窗体的 Windows 窗体应用程序。

具有事件的接口:

Public Interface IDoSomething

Event AddFilter()

模态弹窗实现接口和贴花事件:

Public Class frmPopup Implements IDoSomething

Public Event AddFilter() Implements IDoSomething.AddFilter

弹出窗口还包含触发事件的代码:

RaiseEvent AddFilter()

基本主表单包含发现和启动实现指定接口的弹出表单的代码。

应用程序中的表单启动弹出窗口(实现接口)并处理它触发的任何事件。所以我在表单中有以下代码:

Public Class frmMyForm

Public WithEvents m_Popup As IDoSomething

Public Sub m_Popup_AddFilter() Handles m_Popup.AddFilter

    MsgBox("I'm in")

End Sub

代码一切正常,直到触发事件的阶段。弹出窗口加载没有任何问题,但是当事件触发时,它似乎从地球表面掉了下来,并且没有被主窗体-frmMyForm 拾取。我怀疑这可能与通过发现接口从基本主表单启动弹出表单的方式有关。

附加代码 - 扩展“基本主表单包含发现和启动实现指定接口的弹出表单的代码”:

正在使用的弹出表单的想法是将业务对象返回到使用事件打开它的表单。弹出表单接口 (IDoSomething) 继承了另一个接口 - IBusinessObjectSelector,它指定表单将返回一个业务对象。

所以基本主表单中的功能是:

Public Function GetBusinessObjectUsingPopup(Of O, F As IBusinessObjectSelector)
               (ByRef dicPropertyValues As Dictionary(Of String, Object), 
                Optional ByVal titleText As String = "") 
                As O Implements IBaseMasterForm.GetBusinessObjectUsingPopup

Dim objBusinessObjectSelector As IBusinessObjectSelector = GetPopup(Of F)(False)       
    objBusinessObjectSelector.InitialiseForm()

    ' Activate and show the dialog
    If objBusinessObjectSelector.ShowPopup() <> Windows.Forms.DialogResult.OK Then
        ' The user cancelled the load, so just exit
        Return Nothing
    End If

    GetBusinessObjectUsingPopup = CType(objBusinessObjectSelector.SelectedBusinessObject, O)

End Function

和弹出代码:

Public Function GetPopup(Of F As IBasePopupChildForm)
            (Optional ByVal initialisePopupPriorToReturn As Boolean = True) As F 
             Implements IBaseMasterForm.GetPopup

    Dim lstIBasePopupChildForm As List(Of F) = GetInterfaces(Of F)()
            lstIBasePopupChildForm(0).MyIBaseMasterForm = Me
    If initialisePopupPriorToReturn Then
        lstIBasePopupChildForm(0).InitialiseForm()
    End If
    Return lstIBasePopupChildForm(0)
End Function

注 - GetInterfaces(Of F)() 只是扫描程序集并返回实现所需接口的表单列表。如果找到实现该接口的多个表单,则某些验证已被删除,这些验证将返回消息。

4

1 回答 1

3

关键部分是正确初始化 m_Popup。你什么都没说。一些示例代码:

表格2:

Public Class Form2
    Implements IDoSomething
    Public Event AddFilter() Implements IDoSomething.AddFilter

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent AddFilter()
    End Sub
End Class

Public Interface IDoSomething
    Event AddFilter()
End Interface

表格1:

Public Class Form1
    Private WithEvents mPopup As IDoSomething

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim f2 As New Form2
        f2.Show(Me)
        mPopup = f2
    End Sub

    Private Sub mPopup_AddFilter() Handles mPopup.AddFilter
        MsgBox("yada")
    End Sub
End Class

这段代码中的 mPopup = f2 语句是关键。

于 2009-02-17T17:17:47.120 回答