Click here to Skip to main content
15,569,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check that form is already open or not.
If it is open, then close it and open new someone.

Here is the code, that I'm using and want to rearrange in above case:

SearchFiles searchedFiles = new SearchFiles();
searchedFiles.Owner = App.Current.Windows[0];

Updated 1-Mar-12 14:24pm

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:[^],[^],[^],[^].

Alternatively (and even better, because your form class subclasses System.Window.Forms.Form anyway), override the virtual method OnFormClosing:[^].

Share this answer
If you are using ShowDialog, then you do not need to Close the form - ShowDialog will not return until the form is closed anyway.
So, if you use ShowDialog, you won't get two instances of the form anyway...

If you use Show instead, it will return immediately, and the form will display independently. In this case you could get two instances.

What I would do is keep a class level SearchFiles variable, initially null, and handle the FormClosed event:

if (mySearchFiles != null)
   mySearchFiles = null;
mySearchFiles = new SearchFiles();
mySearchFiles.FormClosed += new FormClosedEventHandler(mySearchFiles_FormClosed);


void mySearchFiles_FormClosed(object sender, FormClosedEventArgs e)
   mySearchFiles = null;
Share this answer
KIDYA 1-Mar-12 4:44am    
Worked.Thanks OriginalGriff!!
OriginalGriff 1-Mar-12 5:33am    
You're welcome!

I have created a static class in which I keep all the public static flag variables so that when any form opens it flags of the variable to true and when the form is closing it unflags it to false.

In this way I can check the variables from different forms to see if the variable is flagged on or off and on that basis of either being on or off I can either ask the first form to show the second form or bring it to focus if it is already open.

This maybe a very unsecured way to expose certain flag variables publicly but works well for me.

I am sure there would be other better ways to do this. Always open to alternate solutions and suggestions.

~ AK
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900