Home > WinForms > C#: WinForms – How to check if form is already open and bring it to the front instead open new instance

C#: WinForms – How to check if form is already open and bring it to the front instead open new instance

20:58

Marek Śliwiński Leave a comment Print This Post  (1,341) Go to comments
       frmOptions formOptions = new frmOptions();

            // open form only if is not open already
            bool isFormOpen = false;

            // iterate through all open forms
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is frmOptions)
                {
                    // open already so just bring it to the front
                    frm.BringToFront();
                    isFormOpen = true;
                    break;
                }
            }

            if (!isFormOpen)
                // not open so show it
                formOptions.Show();
            else
                formOptions.Dispose();
Share and Enjoy:
  • DotNetKicks
  • Digg
  • del.icio.us
  • Wikio IT
  • Google Bookmarks
  • Facebook
  • Print
Categories: WinForms Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Subscribe without commenting