It's a bit weird stuff: there is no such concept as "open" form. You can only check if it is
Visible
(there is such Boolean property) or not. I'm not sure you need to check it though. Please keep reading.
You create a form (with a constructor), show it and then close. If the form is closed (not hidden, which is most usually done with non-main forms through interception of "close by the user" event by handling
FormClosing
, cancellation the event and hiding), but really closed,
it gets disposed, so then you cannot open it again, but can read its properties.
So, you could also handle the event
FormClosing
, or
FormClosed
, let it be closed and set some flag in it which you can test later.
I don't think this is what you really need. You don't really need to know the status of the form. You just need to use appropriate pattern of its usage.
If you use a form using only
ShowDialog
, you should not call
Close
.
Just remove the line with the call to Close
; and it will work just fine. This way, you can simply call
ShowDialog
any number of times. This is the best way of showing a form in a modal way, because if will keep its status.
If you want to show form as non-modal using
Show
, or you need to mix it with
ShowDialog
(by some weird reason), the best pattern is cancellation of
FormClosing
(the event argument parameter has the member
Cancel
you should assign to true if closing reason is
System.Windows.Forms.CloseReason.UserClosing
. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx[
^].
Alternatively (and even better, because your form class subclasses
System.Window.Forms.Form
anyway), override the virtual method
OnFormClosing
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx[
^].
—SA