469,931 Members | 1,831 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,931 developers. It's quick & easy.

How do I obtain Form.Close() result?

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
Nov 10 '05 #1
1 1916
Jules Winfield wrote:
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

Nov 10 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Seth E Seligman | last post: by
5 posts views Thread by Kevin Myers | last post: by
2 posts views Thread by Chien Lau | last post: by
1 post views Thread by Jules Winfield | last post: by
11 posts views Thread by Zytan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.