1

I am just wondering how can I detect when a form is closing from ANOTHER FORM, Say I have my main client open another form open called sender, how would I detect when the sender form is closing from the main client form?

Servy
  • 202,030
  • 26
  • 332
  • 449
Austin
  • 155
  • 1
  • 12
  • Very good suggestion, let me try that, thanks for the fast response! – Austin Sep 23 '14 at 20:51
  • Duplicate of [How to check if a windows form is already open, and close it if it is?](http://stackoverflow.com/a/3861742/342740), you can disregard the close part of the code and focus on the rest – Prix Sep 23 '14 at 20:52
  • @Prix - It is not a duplicate -this is about the form closing. – Enigmativity Sep 23 '14 at 20:54
  • If you've found an answer to your question then post the answer *as an answer to the question* and not as an edit to the question itself. – Servy Sep 23 '14 at 21:30
  • @Enigmativity he said when his SECONDARY form is closing not the main form so yes he can use that question to fit his needs – Prix Sep 23 '14 at 21:38

3 Answers3

3

Attach an event handler to the form's closing event.

This will allow you to do whatever it is you want to do when the form closes.

Chuck Buford
  • 321
  • 1
  • 9
3

You can attach, from the "ANOTHER FORM", an event handler to the FormClosing event

form.FormClosing += (sender, eventArgs) =>
{
    //Do your magic here
};

There is also a System.Windows.Forms.Forms.Closing event but it has been deprecated since .NET 2.0

Carlos Muñoz
  • 17,397
  • 7
  • 55
  • 80
0

It can be done easier than with an event:

if (!otherForm.IsDisposed)
{
    // otherForm is still open
}
Elmue
  • 7,602
  • 3
  • 47
  • 57