How do I obtain Form.Close() result?

J

Jules Winfield

My WinForms application can have any number of top level forms open at a given time. If the user selects File|Exit, all of the forms are closed. The loop to close the forms looks something like this:

foreach(MyForm form in AllMyForms){
form.Close();
}
Application.Exit();

The OnClosing method [aka OnFormClosing in .NET 2.0] is overridden in MyForm. If the user has made changes to his form, this method asks him if he'd like to:
a) save his changes
b) forget changes and proceed to close the form
c) cancel the form closure loop and keep working on the form

In the event that the user chooses (c), OnClosing will cancel the closure operation. What I don't understand is why Form.Close() returns void. It seems like it should return a bool indicating whether or not the Close() operation was completed. Am I missing something? Given a call to Form.Close(), how can I tell if the form closure actually succeeded.

The only idea I can come up with is to check the Form.IsDisposed property immediately following the Close() call, with the assumption that if my modeless Form truly did close, it should also be Disposed. Is this the recommended approach?

Jules





In the
 
C

Chris Dunaway

Jules said:
The OnClosing method [aka OnFormClosing in .NET 2.0] is overridden in MyForm. If

Just to be clear, you should not use OnClosing in .Net 2.0, you should
use OnFormClosing instead as OnClosing is obsolete and may be removed
in the future. (but I assume you know this).
The only idea I can come up with is to check the Form.IsDisposed property immediately

What about handling the FormClosed event? This event is called after
the form has closed. Wherever you instantiate the form , wire up the
FormClosed event and then you will be notified when the form has
closed. This event won't be called if the closing is cancelled.

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top