Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 0.77 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Close all open forms except the main menu in c#
  2. FormCollection formsList = Application.OpenForms;
  3.        
  4. if (thisForm.Name != "Menu") thisForm.Close();
  5.        
  6. List<Form> openForms = new List<Form>();
  7.  
  8. foreach (Form f in Application.OpenForms)
  9.     openForms.Add(f);
  10.  
  11. foreach (Form f in openForms)
  12. {
  13.     if (f.Name != "Menu")
  14.         f.Close();
  15. }
  16.        
  17. for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
  18. {
  19.     if (Application.OpenForms[i].Name != "Menu")
  20.         Application.OpenForms[i].Close();
  21. }
  22.        
  23. for (int i = formsList.Count-1; i > 0; i--)
  24. {
  25.     if (formsList[i].Name != "Menu")
  26.     {
  27.         formsList[i].Close();
  28.     }
  29. }
  30.        
  31. Form[] forms = Application.OpenForms.Cast<Form>().ToArray();
  32. foreach (Form thisForm in forms)
  33. {
  34.     if (thisForm.Name != "Menu") thisForm.Close();
  35. }