[英]Close all Forms except my main Form
I'm using WinForms
. 我正在使用WinForms
。 I want to close all of the forms except my main form called Form1
. 我想关闭除我的主窗体Form1
之外的所有窗体。 I noticed that my main Form is index 0, so i was wondering if i could do something like, close all the forms except of index 0. How can i do this? 我注意到我的主要表格是索引0,所以我想知道是否可以做类似的事情,关闭除索引0以外的所有表格。我该怎么做? This is what i have so far. 这是我到目前为止所拥有的。
List<Form> openForms = new List<Form>();
foreach (Form f in Application.OpenForms)
{
openForms.Add(f);
int mainFormIndex = openForms.IndexOf(0);
Console.WriteLine(": " + mainFormIndex);
if(mainFormIndex != 0)
{
this.Owner.Close();
}
else
{
this.Close();
}
}
}
You can close all forms except Form1
instance using linq this way: 您可以使用linq关闭除Form1
实例以外的所有表单:
Application.OpenForms.Cast<Form>().Where(x => !(x is Form1))
.ToList().ForEach(x => x.Close());
You can check the name of Form and then close, for example if you need to keep Form1 open and close all other forms; 您可以检查Form的名称,然后关闭,例如,如果需要保持Form1打开并关闭所有其他表单,例如:
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "Form1")
{
Application.OpenForms[i].Close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.