1 #region 关闭前面的窗体打开新窗体
2 //关闭前面的窗体
3 private void ClosePreForm()
4 {
5 //首先判断当前容器中是否存在窗体
6 foreach (Control item in this.spContainer.Panel2.Controls)
7 {
8 if (item is Form)
9 {
10 Form objControl = (Form)item;
11 objControl.Close();
12 }
13 }
14 }
15
16 //打开窗体
17 private void OpenForm(Form objFrm)
18 {
19 objFrm.TopLevel = false;//将子窗体设置为*控件
20 objFrm.WindowState = FormWindowState.Maximized;//让子窗体最大化
21 objFrm.FormBorderStyle = FormBorderStyle.None;//去掉窗体的边框
22 objFrm.Parent = this.spContainer.Panel2;//定义子窗体显示的容器
23 objFrm.Show();
24 }
25 #endregion
调用的话
1 private void btnAddStudent_Click(object sender, EventArgs e) 2 { 3 ClosePreForm(); 4 OpenForm(new FrmAddStudent()); 5 }