Trying to close all forms except for the main menu using
FormCollection formsList = Application.OpenForms;
with a foreach loop and saying,
I know this is old but I needed to perform this same scenario and came up with a elegant and simple way to achieve this as follows
Form[] formsList = Application.OpenForms.Cast().Where(x => x.Name == "Form1").ToArray(); foreach (Form openForm in formsList) { openForm.Close(); }
This will close ALL windows that where opened called Form1