My scenario:
Windows Forms Application with a base master (mdi) form.
An Interface that has an event:
Public Interface IDoSomething 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.
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.
The critical part is initializing m_Popup correctly. You haven’t said anything about that. Some sample code:
Form2:
Form1:
The mPopup = f2 statement in this code is key.