繁体   English   中英

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

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

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

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

内部按钮单击事件

{
    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