Check if form already Open

  • Thread starter Thread starter KelsMckin
  • Start date Start date
K

KelsMckin

Hello, I was wondering if there was a way to check there was already a
form open before opening a new one, sorry that doesnt seem clear, here
is an example:

The following button opens a new form

private void btnAbout_Click(object sender, EventArgs e)
{
avernusAbout About = new avernusAbout();
About.StartPosition = FormStartPosition.Manual;
About.Location = new Point(this.Location.X,
this.Location.Y);
About.Show();
this.Hide();
}

is there a way to check if the form is already open before It opens the
new form?

Any help would be greatly appreciated
 
If you are using .NET 2.0, use Application.OpenForms as in:

foreach (Form a in Application.OpenForms)
{
if (a is avernusAbout)
{
// About form is open
break;
}
// About form is not open...
}

Hello, I was wondering if there was a way to check there was already a
form open before opening a new one, sorry that doesnt seem clear, here
is an example:

The following button opens a new form

private void btnAbout_Click(object sender, EventArgs e)
{
avernusAbout About = new avernusAbout();
About.StartPosition = FormStartPosition.Manual;
About.Location = new Point(this.Location.X,
this.Location.Y);
About.Show();
this.Hide();
}

is there a way to check if the form is already open before It opens the
new form?

Any help would be greatly appreciated
 
Maybe I should have provided you with a tad bit more information, what
I needed to do if the form was currently open is show it. How could I
go about doing that?
 
Maybe I should have provided you with a tad bit more information, what
I needed to do if the form was currently open is show it. How could I
go about doing that?

Why not make the Form a singleton? Something like this:

public class MyForm : System.Windows.Forms.Form
{
private static MyForm _instance = null;

private void MyForm()
{
... usual constructor stuff ...
}

public MyForm Instance
{
get
{
if (MyForm._instance == null)
{
MyForm._instance = new MyForm();
}
return MyForm._instance;
}
}
}

Now, whenever you want to show the form, just say:

MyForm.Instance.Show();

No need to search, and it works in .NET 1.1 and 2.0. Will that work for
you?
 
Bruce, I am not sure how to implement that singleton, however I will do
some research on the subject, thankyou all for your help
 
Maybe I should have provided you with a tad bit more information, what
I needed to do if the form was currently open is show it. How could I
go about doing that?

To use the previous poster's example (which is a good one):

foreach (Form a in Application.OpenForms)
{
if (a is avernusAbout)
{
a.TopMost = true; // Moves it to the top of the window stack
// or (if it is simply not visible)
a.Visible = true;
}
// About form is not open...

}

Matt
 
Matt's code should do the trick.

Maybe I should have provided you with a tad bit more information, what
I needed to do if the form was currently open is show it. How could I
go about doing that?
 
a.TopMost = true; // Moves it to the top of the window stack

IIRC, this toggles the "always on top" behaviour; a combination of
Activate(), Focus() and BringToFront() would probably be more in order,
otherwise the user could find it hard to get rid of the form (to the
background) afterwards.

Marc
 
Bruce Wood wrote:

[...]
Why not make the Form a singleton? Something like this:
[...]

This will work if there'll only ever be one instance of that form. But
sometimes, you'll have to make sure there's only one instnce of a certain
form per business object, e. g. to open a customer form once per customer
and bring it to top otherwise.

We're keeping a Dictionary of our opened forms with a combination of form
class name and business object id as a key and look it up whenever we want
to open a new view, then proceed pretty much as you did...
 

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

Back
Top