练习:WinForm(控件Button,打开,关闭窗体)
Form1.cs
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { //在内存中创建form2对象 Form2 f2 = new Form2(); //展示当前窗体 f2.Show(); } /// <summary> /// 当加载窗体的时候,将窗体的对象放到Class1类中的静态字段中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { Class1._fTest = this; } } }
Form2.cs
namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form3 f3 = new Form3(); f3.Show(); } } }
Form3.cs
namespace WindowsFormsApplication1 { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Class1._fTest.Close(); } } }
Class1.cs
namespace WindowsFormsApplication1 { class Class1 { public static Form1 _fTest; } }