在MDI形式中以c sharp形式关闭子表单

时间:2011-03-28 06:48:13

标签: c#

我在c尖锐按钮点击事件中创建了具有相同瞬间的新起点。但所有人都有不同的名字。我将这些名称存储在一个数组中。

最后我需要关闭MDI中的一些选定的。我怎么能这样做..?

private List<string> FormName = new List<string>();
private Form newPersonForm = null;
点击事件()

中的

{
    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);
    }
}

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

存储Form个对象,而不是string

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

处理程序:

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

答案 2 :(得分:1)

内部按钮点击事件

{
    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);
    }
}