C#多窗体关闭
程序员文章站
2022-06-10 12:42:58
...
//新建一个Test类, 静态类, 静态字段可以共享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _C_2020_11_16
{
public static class Test //static
{
public static Form1 _fr1Test; //字段
}
}
在Form 1中,打开form2, 并将Form1_Load存入test中
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
/// <summary>
/// 当加载窗体时,将窗体对象放到Test类中的静态字段中
/// </summary>
{
Test._fr1Test = this;
}
在Form2中打开Form3
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
}
Form3中关闭主窗口
private void button1_Click(object sender, EventArgs e)
{
Test._fr1Test.Close();
}```