How to check if a windows form is already open, and close it if it is?

Viewed 263173

I have a form "fm" that is a simple info window that opens every 10 mins (fm.Show();).

How I can make that every 10 mins it will check if the form "fm" is open and if it is open it closes it and open it again!

Now the form fm is always created with form fm = new form();
so when I try to check if the form is open it will always be false and open a new window even if there is one form before!

I need to have a tool to give it a unique identity and then check if this form with unique identity is opened or not!

I do not want to just update the data on the form (fm), because I have a complicated info with buttons.

The form name is "UpdateWindow"

Thanks

29 Answers
Form fc = Application.OpenForms["UpdateWindow"]; 

if (fc != null) 
   fc.Close(); 

fc.Show();

try this , no need to iterate through the forms :

if(Application.OpenForms["<your_form_name>"] != null){

   //Your form is already open

}
else {

  //Your form isn't open

}

The below actually works very well.

private void networkInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
    var _open = false;
    FormCollection fc = Application.OpenForms;
    foreach (Form frm in fc)
    {
        if (frm.Name == "FormBrowseNetworkInformation")
        {
            _open = true;
            frm.Select();
            break;
        }
    }
    if (_open == false)
    {
        var formBrowseNetworkInformation = new FormBrowseNetworkInformation();
        formBrowseNetworkInformation.Show();
    }
}

this will word definitely. i use this function for myself as well.

  public static bool isFormOpen(Form formm)
    {

        foreach (Form OpenForm in Application.OpenForms)
        {
            if (OpenForm.Name == formm.Name)
            {
                return true;
            }
        }

        return false;
    }

I've tweaked an earlier post I made. This work flawlessly without having to iterate though all open forms.

        Form fc = Application.OpenForms["FormBrowse"];
        if (fc != null)
        {
            fc.Select();
        }
        else
        {
            var formBrowse = new FormBrowse();
            formBrowse.Show();
        }

This works if you want to Check if the Second Form is already Open and avoidt opening it again on buttong Click.

 int formcheck = 0;
    private void button_click()
    {
       Form2Name myForm2 = new Form2Name();
       if(formcheck == 0)
       {
          myForm2.Show(); //Open Form2 only if its not active and formcheck == 0
          // Do Somethin

          formcheck = 1; //Set it to 1 indicating that Form2 have been opened
       {   
    {

You can use ShowDialog() instead of using Show()

Something like this:

var newView = (viewForm)Program.ServiceProvider.GetService(typeof(viewForm));
 newView.ShowDialog();

For example:

private bool CheckOpened<T>() where T : Form 
{
    bool result = false;
    FormCollection fs = Application.OpenForms;            

    foreach (var f in fs)
    {
        if (f is T)
        {
            result = true;
            break;
        }
    }

    return result;
}

private void ShowDashboard()
{
    if (!CheckOpened<FormMain>())
    {
        var fMain = new FormMain();
        fMain.Show();
    }                       
}
Related