简体   繁体   English

在C Sharp中以MDI格式关闭子froms

[英]Closing child froms in MDI form in c sharp

I create new froms with same instant in c sharp button click event. 我在c快捷按钮单击事件中创建具有相同瞬间的新消息。 But all have different names. 但是所有人都有不同的名字。 I store those names in an array. 我将这些名称存储在数组中。

At last i need to close some selected froms in the MDI. 最后,我需要关闭MDI中的某些选定项目。 How can i do that..? 我怎样才能做到这一点..?

private List<string> FormName = new List<string>();
private Form newPersonForm = null;

in click event() 点击事件中

{
    personId = getFromOutside;
    if(FormName.contains(personId))
    {
        here i need to close the form related to the person id ?
    }
    else
    {
        newPersonForm = new Form();
        newPersonFrom.Name=personId;
        FormName.add(personId);
    }
}
// Assume that your parent window is parentForm
// When you are creating each form, add it to parentForm's owned forms
// Let's say you're creating form1

MyForm form1 = new MyForm();
parentForm.AddOwnedForm(form1);//if you're in your parentForm's class use this.AddOwnedForm(form1);
form1.Show();

// then when you want to close all of them simple call the below code

foreach(Form form in parentForm.OwnedForms)
{
      form.Close();
}

// And also you can call this if you're in parentForm's class
foreach(Form form in this.OwnedForms)
{
      form.Close();
}

Store Form objects, not string : 存储Form对象,而不是string

private List<Form> Forms = new List<Form>();

handler: 处理:

personId = getFromOutside;
var existingForm = Forms.FirstOrDefault(f => f.Name == personId);
if(existingForm != null)
{
    existingForm.Close();
    Forms.Remove(existingForm);
}
else
{
    newPersonForm = new Form();
    newPersonFrom.Name=personId;
    Forms.Add(newPersonForm);
}  

Inside button click event 内部按钮单击事件

{
    personId = getFromOutside;
    if(FormName.contains(personId))
    {
        //here i need to close the form related to the person id ?

        foreach (Form f in Application.OpenForms)
        {
            if (f.Text == personId.ToString()) //  compare Name of the Form
            {
                f.Close();
                break;
            }
        }
    }
    else
    {
        newPersonForm = new Form();
        newPersonFrom.Name=personId;
        FormName.add(personId);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM