C#: Check if a Form is already opened

During WinForms programming you might (chances are that you SHALL) come across a situation where you do not want to open a form multiple times on an event.  A common practice in the event handler function is:

UserForm UForm = new UserForm();
UForm.Show();

This will open your form, fine! But what if the event happens again and the previously opened UserForm has not been closed yet? A new UserForm will be opened and this will go on if the user loves that event  ^o 😐

The prevention strategy is to check whether the form is already opened or not.

public static Form IsFormAlreadyOpen(Type FormType)
{
   foreach (Form OpenForm in Application.OpenForms)
   {
      if (OpenForm.GetType() == FormType)
         return OpenForm;
   }

   return null;
}

This little function will help in determining if a particular form is already opened or not.  You can open it or focus it later.

UserForm UForm = null;
if ((UForm = IsFormAlreadyOpen(typeof(UserForm)) == null)
{
    UForm = new UserForm();
    UForm.Show();
}
else
{
    UForm.DoWhatever(); // may be UForm.Select();
}

This works perfect for me. I don’t like that loop though.

Advertisements

22 thoughts on “C#: Check if a Form is already opened

  1. Pingback: C# Winforms: Create a Single Instance Form « Hash

  2. Your example was not working for me, the function was ok, but I modified the way check if opened:

    UserForm frm = null;
    if ((frm = (UserForm)IsFormAlreadyOpen(typeof(UserForm))) == null)
    {
    frm = new UserForm();
    frm.Show();
    }
    else
    MessageBox.Show(“Already Opened”);

  3. here is how i do it:

    bool onsiteopen = false;

    foreach (Form S in Application.OpenForms)
    {
    if (S is “MyForm” )
    {
    onsiteopen = true;

    }

    }
    if (onsiteopen == true)
    {
    MessageBox.Show(“this form is already open.”);
    }

  4. oops i left some out….

    bool FormOpen= false;
    foreach (Form
    S in Application.OpenForms)
    {
    if (S is MyForm)
    {
    FormOpen= true;

    }

    }
    if (FormOpen== true)
    {
    MessageBox.Show(“Form already open.”);
    }
    else
    {
    Form MyForm= new MyForm();
    OnSite.Show();

    }

  5. if (newFrmReports.IsMdiChild && newFrmReports.IsDisposed==false)
    {

    newFrmReports.WindowState = FormWindowState.Normal;
    newFrmReports.BringToFront();
    }
    else
    {

    newFrmReports = new frmReports();
    newFrmReports.MdiParent = this;
    newFrmReports.Show();

    }

  6. Thanks man, Nice Coding. I used Like This,

    Form TaskFormName;
    if ((TaskFormName = IsFormAlreadyOpen(typeof(MyForm))) == null)
    {
    TaskFormName = new MyForm(null);
    if (ActiveForm != null) TaskFormName.MdiParent = ActiveForm;
    TaskFormName.Show();
    }
    else
    {
    TaskFormName.WindowState = FormWindowState.Normal;
    TaskFormName.BringToFront();
    }

  7. You can also use Application.OpenForms[string]
    where string is the name of your class

    You may want to store this in a const so you only have to change it once in case the class name changes

    public static bool IsFormRunning()
    {
    Form f = Application.OpenForms[“Form1”];

    return f != null;
    }

  8. Pingback: c sharp check if form is already opened « the gravel project

  9. Hi all,
    We have a scenario where in a single form is opened via a multiple menu commands of a MDI Form. However, I have created separate instances of that form. But, there is method, that checks if the form is open as under:
    private bool IsMDIChildFormOpen(string formName)
    {
    try
    {
    foreach (Form childForm in this.MdiChildren)
    {
    if (childForm.Name == formName)
    {
    if (childForm == null || childForm.IsDisposed)
    return false;
    else
    return true;
    }
    }
    }
    catch (Exception ex)
    {
    PowerTraderException.WriteCustomError(“IsMDIChildFormOpen() : ” + ex.StackTrace);
    throw ex;
    }
    return false;
    }

    and in the menu command handler we check it using:

    private void frmMainToolStripMenuItem_Click(object sender, EventArgs e)
    {
    try
    {

    if (!IsMDIChildFormOpen(“frmMyForm”))
    {
    objMyForm = new frmMyForm();
    objMyForm.MdiParent = this;
    objMyForm.Show();
    }
    else
    {
    if (IsMDIChildFormOpen(“frmMyForm”))
    {
    objMyForm.BringToFront();
    }
    }
    }
    catch (Exception ex)
    {

    }
    }

    It works for all other forms, except one where the same form is opened from 5 different menu command handlers. I want a mechanism to differentiate these instances of the same form and identify which one is open and which instance should be newed.

    Any pointers are much appreciated.

    Thanks.

  10. public void ControlCheck(Control ctl,TabControl tControl)
    {
    ctl.Dock = DockStyle.Fill;
    string cname = ctl.Text;
    if (tControl.HasChildren==true)
    {
    for (int i = 0; i < tControl.TabPages.Count; i++)
    {
    if (tControl.TabPages[i].Text == cname)
    {
    tControl.SelectedTabPage = tControl.TabPages[i];
    return;
    }
    }
    }
    TabPage pg = new XtraTabPage();
    pg.Text = cname;
    pg.Controls.Add(ctl);
    tControl.TabPages.Add(pg);
    ctl.Show();
    }

  11. oops left something out sorry >>> Correct Code Below>>>
    public void ControlCheck(Control ctl,XtraTabControl tControl)
    {
    ctl.Dock = DockStyle.Fill;
    string cname = ctl.Text;
    if (tControl.HasChildren==true)
    {
    for (int i = 0; i < tControl.TabPages.Count; i++)
    {
    if (tControl.TabPages[i].Text == cname)
    {
    tControl.SelectedTabPage = tControl.TabPages[i];
    return;
    }
    }
    }
    TabPage pg = new TabPage();
    pg.Text = cname;
    pg.Controls.Add(ctl);
    tControl.TabPages.Add(pg);
    ctl.Show();
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s