=======================遍历所有已打开的窗体,然后关闭==================


 
  
  1. private void button1_Click(object sender, EventArgs e) 
  2.        { 
  3.            //遍历所有已打开的窗体,然后关闭 
  4.            foreach (Form frm in Application.OpenForms) 
  5.            { 
  6.                if (frm.GetType().ToString() == "WindowsFormsApplication1.Form4"
  7.                { 
  8.                    MessageBox.Show(frm.GetType().ToString()); 
  9.                    form = frm; 
  10.  
  11.                } 
  12.            } 
  13.            if (form != null
  14.            { 
  15.                form.Close(); 
  16.            } 
  17.        } 

===================以下是在本窗口打开窗口再关闭的代码==============

 

 
  
  1. public Form form = null
  2. private void button3_Click(object sender, EventArgs e) 
  3.         {//关闭窗体 
  4.             if (form != null
  5.             { 
  6.                 form.Close(); 
  7.                 form = null
  8.             } 
  9.         } 
  10.         private void button2_Click(object sender, EventArgs e) 
  11.         {//显示窗体 
  12.             if (form == null
  13.             { 
  14.                 form = new Form4(); 
  15.                 form.Show(); 
  16.             } 
  17.         }