Form1 objForm1 = new Form1 ();
objForm1 .MdiParent = this;
objForm1 .Show();
This is my code to open MDI form . IF I open this page again, it appears again and again and so many windows opens. Can anybody help?
If you want to create a new form only if it is not already opened you can do this:
ShowFormIfNotOpen(this,typeof(Form1));
public static void ShowFormIfNotOpen(Form mainform,Type type)
{
foreach (Form item in mainform.MdiChildren)
if (item.GetType() == type)
{
item.Activate();
return;
}
Form form = Activator.CreateInstance(type) as Form;
form.MdiParent = mainform;
form.Show();
}
Update
1)Add a public static bool field/property in your form (IsAlreadyShown)
public static bool IsAlreadyShown { get; set; }
2)Set it to true in the constructor of the form
public Form1()
{
InitializeComponent();
IsAlreadyShown = true;
}
3)Call ShowForm1(this);
public static void ShowForm1(Form parentForm)
{
if(Form1.IsAlreadyShown ==true)
return;
Form1 objForm1 = new Form1 ();
objForm1 .MdiParent = parentForm;
objForm1 .Show();
}