Prevent duplicate MDI children forms

user picture user · Oct 12, 2009 · Viewed 21.8k times · Source

Is there a way to prevent the opening of a certain form within an MDI container if that said form is already opened?

Answer

Fredrik Mörk picture Fredrik Mörk · Oct 12, 2009

You can interate over the OpenForms collection to check if there is already a form of the given type:

foreach (Form form in Application.OpenForms)
{
    if (form.GetType() == typeof(MyFormType))
    {
        form.Activate();
        return;
    }
}

Form newForm = new MyFormType();
newForm.MdiParent = this;
newForm.Show();