Social Icons

Pages

Wednesday, October 12, 2011

Easiest way to detect an open form in C#

During winforms programming, where you do not want to open a form multiple forms.
Here is a way to check there is already a form open before opening a new one.
public partial class MainForm : Form
{     
        private Frm_ Form frm_Form;

private void OpenForm()           
       {

            if (frm_Form != null)               
            {
                if (frm_Form.IsDisposed == false)
                {
                   frm_Form.Activate();
                }
                else
                {
                   frm_Form = new Frm_ Form ();
                   frm_Form.Show();
                }
            }
            else
            {
                   frm_Form = new Frm_ Form ();
                   frm_Form.Show();
            }
              

       }
}

No comments:

Post a Comment