How to inform one form that one another form has just closed?

pisceswzh

Well-known member
Joined
Mar 19, 2007
Messages
96
Programming Experience
1-3
Here is the simplified problem:

I have got a form, let's name it formMain, and there is a button on formMain, let's name this button btnMain. Now, I want to click btnMain and popup another form (the popup should be triggled by the command .Show, not .ShowDialog), let's name this form formSub and when the formSub is closed, how can formMain be notified and triggle a subroutine on the formMain?

Thanks!
 
The 'quickest' method that I can think of is to create a public shared boolean in formMain, have formSub change that boolean to true (as in yes the form is open) then have it set it too false when it closes.

This is much less efficient and less flexible then what jmcilhinney put forward though.
 
The 'quickest' method that I can think of is to create a public shared boolean in formMain, have formSub change that boolean to true (as in yes the form is open) then have it set it too false when it closes.

This is much less efficient and less flexible then what jmcilhinney put forward though.

Then what.. have a timer to check that? Ugh!


VB.NET:
Public Sub MyForm1Handler(blah..)
  MessageBox.Show("Form2 has closed")
End Sub


Dim f as New Form2
AddHandler f.FormClosed, AddressOf Me.MyForm1Handler

f.Show()
 
The 'quickest' method that I can think of is to create a public shared boolean in formMain, have formSub change that boolean to true (as in yes the form is open) then have it set it too false when it closes.

This is much less efficient and less flexible then what jmcilhinney put forward though.

So what if you have more than one formSub?

cjard's suggestion is the proper one in this case. Use AddHandler when instantiating the new form to handle its FormClosed event. It will never fail, and can handle any number of forms.
 
Back
Top