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.


9 responses so far ↓
C# Winforms: Create a Single Instance Form « Hash // February 6, 2009 at 6:12 pm
[...] never created and shown. I have previously written a small note on this topic and gave a solution here which I personally don’t [...]
hashfactor // February 6, 2009 at 6:13 pm
If you want to try a Singleton Pattern to keep one instance of the Form, here it is
http://hashfactor.wordpress.com/2009/02/06/c-winforms-create-a-single-instance-form/
Gonzalo Lebron // June 19, 2009 at 9:43 am
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”);
Rejitha // June 25, 2009 at 4:51 pm
Excellent!! this works!!
Jay Stratemeyer // July 18, 2009 at 3:43 am
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.”);
}
Jay Stratemeyer // July 18, 2009 at 3:45 am
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();
}
Luis // August 17, 2009 at 8:40 pm
if (newFrmReports.IsMdiChild && newFrmReports.IsDisposed==false)
{
newFrmReports.WindowState = FormWindowState.Normal;
newFrmReports.BringToFront();
}
else
{
newFrmReports = new frmReports();
newFrmReports.MdiParent = this;
newFrmReports.Show();
}
Ravindr.pc // August 19, 2009 at 4:04 pm
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();
}
SSK Sorgulama // November 10, 2009 at 5:59 pm
Thanks Gonzalo, it works for me