I create new froms with same instant in c sharp button click event. But all have different names. I store those names in an array.
At last i need to close some selected froms in the 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
:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.