Close all open forms except the main menu in C#

前端 未结 8 1905
终归单人心
终归单人心 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:09

    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

提交回复
热议问题