Prevent duplicate MDI children forms

前端 未结 7 732
攒了一身酷
攒了一身酷 2020-12-09 11:47

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

7条回答
  •  -上瘾入骨i
    2020-12-09 12:18

    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();
    

提交回复
热议问题