I am trying to close a form if it is already opened, this form is the main of the application, so if I close all the instances of this form, the application ends. I am trying to look for if that form is opened, and I find it, close it if it is more than one opened:
With this I get the instances opened with the name "Main":
int nPrincOpen = openForms
.OfType<Form>()
.Where(form => String.Equals(form.Name, "Main"))
.ToList()
.Count();
And with this I close these forms, but I close all the forms:
for (int i = 0; i < nPrincOpen - 1; i++)
{
Application.OpenForms
.OfType<Form>()
.Where(form => String.Equals(form.Name, "Main"))
.ToList()
.ForEach(form => form.Close());
}
How could I close all the forms except one using link?