views:

440

answers:

1

Hi All,

My scenario:

  • Windows Forms Application with a base master (mdi) form.

  • Public Interface IDoSomething that has an event:

    Event AddFilter()

  • Modal popup window implements the interface and decalres event:

    Public Class frmPopup Implements IDoSomething

    Public Event AddFilter() Implements IDoSomething.AddFilter

  • Popup also contains code to fire the event:

    RaiseEvent AddFilter()

  • Base master form contains code that discovers and launches popup forms that implement a specified interface.

  • A form in the application launches the popup (that implements the interface) and handle any events that it fires. So I have the following code in the form:

    Public Class frmMyForm

    Public WithEvents m_Popup As IDoSomething

    Public Sub m_Popup_AddFilter() Handles m_Popup.AddFilter

    MsgBox("I'm in")
    

    End Sub

The code is all working, up until the stage where the event is fired. The popup loads without any issues but when the event fires it seems to drop off the face of the earth and is not being picked up by the main form - frmMyForm. I suspect it may have something to do with the way the popup form is being launched from the base master form via the discovery of the interface. Any help would be welcome as I've been stuck with this for nearly 2 days!!!!! TIA

ADDITIONAL CODE - to expand on "Base master form contains code that discovers and launches popup forms that implement a specified interface":

The idea of the popup forms that are being used is to return a business object to the form that opened it using events. The popup form Interface (IDoSomething) inherits another interface - IBusinessObjectSelector which specifies that the form will return a business object.

So the function in the base master form is:

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

And Popup Code:

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

Note - GetInterfaces(Of F)() simply scans the assembly and returns a list of forms that implement the required interface. Some validation has been chopped out that returns messages if multiple forms that implement the interface are found.

+1  A: 

The critical part is initializing m_Popup correctly. You haven't said anything about that. Some sample code:

Form2:

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

Form1:

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

The mPopup = f2 statement in this code is key.

Hans Passant
Thanks for your response, I've added some additional code above to try to show how the popup is being launched, hopefully it will make it a bit clearer.
Tanner
I still don't see you assigning the m_Popup member.
Hans Passant
Just to update you on progress, your comment about assigning m_Popup is valid and there's no handle back to the original opening form to tie the events up. It's currently been raised as a bug, so when i get to it and implement a fix, I'll update posting and credit you. Thanks
Tanner