Close all open forms except the main menu in C#

前端 未结 8 1891
终归单人心
终归单人心 2020-12-16 14:27

Trying to close all forms except for the main menu using

FormCollection formsList = Application.OpenForms;

with a foreach loop and saying,

8条回答
  •  甜味超标
    2020-12-16 15:08

    Collection was modified; enumeration operation may not execute.

    FormCollection formsList = Application.OpenForms;
                //for (int i = 0; i < formsList.Count; i++)
                foreach(Form  f in formsList )
                {
                    if (f.Name != "Form1" || f.Name != "Home" || f.Name != "AdminHome")
                        f.Close();
                }
                this.Close();
    

提交回复
热议问题