=======================遍历所有已打开的窗体,然后关闭==================
- private void button1_Click(object sender, EventArgs e)
- {
- //遍历所有已打开的窗体,然后关闭
- foreach (Form frm in Application.OpenForms)
- {
- if (frm.GetType().ToString() == "WindowsFormsApplication1.Form4")
- {
- MessageBox.Show(frm.GetType().ToString());
- form = frm;
- }
- }
- if (form != null)
- {
- form.Close();
- }
- }
===================以下是在本窗口打开窗口再关闭的代码==============
- public Form form = null;
- private void button3_Click(object sender, EventArgs e)
- {//关闭窗体
- if (form != null)
- {
- form.Close();
- form = null;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {//显示窗体
- if (form == null)
- {
- form = new Form4();
- form.Show();
- }
- }
转载于:https://blog.51cto.com/lionkas/1093422