防止重复的MDI子表单

use*_*ser 12 c# mdi children

有没有办法阻止在MDI容器中打开某个表单,如果该表单已经打开?

Fre*_*örk 35

您可以对OpenForms集合进行交互,以检查是否已存在给定类型的表单:

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

Form newForm = new MyFormType();
newForm.MdiParent = this;
newForm.Show();
Run Code Online (Sandbox Code Playgroud)