可以阻止窗体关闭改为隐藏,具体看下面的代码,帮助到你可以点击采纳支持下,谢谢~~
List<Form> formList1, formList2;
private void button2_Click(object sender, EventArgs e)
{
if (formList1 != null) foreach (var f in formList1) { f.Hide(); }
if (formList2 == null)
{
formList2 = new List<Form> { new Form2a { }, new Form2b { } };
foreach (var f in formList2) f.FormClosing += F_FormClosing;//添加关闭前事件阻止窗体关闭改为隐藏窗体
}
foreach (var f in formList2) f.Show();
}
private void F_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;//禁止关闭
var f = (Form)sender;
f.Hide();//隐藏窗体
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (formList2 != null) foreach (var f in formList2) { f.Hide(); }
if (formList1 == null)
{
formList1 = new List< Form> { new Form1a { } , new Form1b { } };
foreach (var f in formList1) f.FormClosing += F_FormClosing;//添加关闭前事件阻止窗体关闭改为隐藏窗体
}
foreach (var f in formList1) f.Show();
}