(一)标签页
先看看效果:
代码:
1 public partial class 标签页 : Form 2 { 3 string s = ""; 4 public 标签页() 5 { 6 InitializeComponent(); 7 } 8 /// <summary> 9 /// 判断选项卡中是否有本选项卡 10 /// </summary> 11 /// <param name="s">选项卡名</param> 12 /// <returns>布尔型</returns> 13 14 private bool HasTablePages(string s) 15 { 16 for (int i = 0; i < tabControl1.TabPages.Count; i++) 17 { 18 if (tabControl1.TabPages[i].Text == s) 19 { 20 tabControl1.SelectedIndex = i;//如果选项卡中有本选项卡,则把本选项卡设为焦点 21 return false; 22 } 23 } 24 return true; 25 } 26 /// <summary> 27 /// 点击菜单时,创建本选项卡,同时将自定义的选项卡控件添加进去 28 /// </summary> 29 /// <param name="s">选项卡名</param> 30 /// <param name="uc">自定义的控件</param> 31 private void MenuAdd(string s,UserControl uc) 32 { 33 if (HasTablePages(s)) 34 { 35 TabPage page = new TabPage(); 36 page.Text = s; 37 38 page.Controls.Clear(); 39 page.Controls.Add(uc); 40 uc.Dock = DockStyle.Fill; 41 42 tabControl1.TabPages.Add(page); 43 tabControl1.SelectedTab = page; 44 } 45 } 46 private void 会员管理ToolStripMenuItem_Click(object sender, EventArgs e) 47 { 48 s = "会员管理"; 49 UC_HuiYuan uc = new UC_HuiYuan(); 50 MenuAdd(s,uc); 51 } 52 53 private void 数据管理ToolStripMenuItem_Click(object sender, EventArgs e) 54 { 55 s = "数据管理"; 56 UC_ShuJu sj = new UC_ShuJu(); 57 MenuAdd(s,sj); 58 } 59 60 private void 客房管理ToolStripMenuItem_Click(object sender, EventArgs e) 61 { 62 s = "客房管理"; 63 UC_KeFang kf = new UC_KeFang(); 64 MenuAdd(s,kf); 65 } 66 /// <summary> 67 /// 双击选项卡名时,关闭本选项卡 68 /// </summary> 69 /// <param name="sender"></param> 70 /// <param name="e"></param> 71 private void tabControl1_DoubleClick(object sender, EventArgs e) 72 { 73 tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex); 74 } 75 }
(二)子窗体
先看看效果:
代码:
1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 } 7 /// <summary> 8 /// 关闭所有窗口 9 /// </summary> 10 /// <param name="sender"></param> 11 /// <param name="e"></param> 12 private void 关闭所有窗口ToolStripMenuItem_Click(object sender, EventArgs e) 13 { 14 while (this.MdiChildren.Length > 0) 15 { 16 MdiChildren[0].Close(); 17 } 18 } 19 /// <summary> 20 /// 新建Form2 21 /// </summary> 22 /// <param name="sender"></param> 23 /// <param name="e"></param> 24 private void ToolStripMenuItem_Click(object sender, EventArgs e) 25 { 26 bool b = true;//用来判断子窗口中是否存在Form2 27 for (int i = 0; i < this.MdiChildren.Length; i++) 28 { 29 if (this.MdiChildren[i].Name == "Form2") 30 { 31 b = false; 32 } 33 } 34 //没有子窗口 或者 子窗口中没有Form2 时 35 if (this.MdiChildren.Length == 0 || b) 36 { 37 Form2 fm = new Form2(); 38 fm.MdiParent = this; 39 fm.Show(); 40 } 41 } 42 /// <summary> 43 /// 新建Form3 44 /// </summary> 45 /// <param name="sender"></param> 46 /// <param name="e"></param> 47 private void toolStripMenuItem2_Click(object sender, EventArgs e) 48 { 49 bool b = true; 50 for (int i = 0; i < this.MdiChildren.Length; i++) 51 { 52 if (this.MdiChildren[i].Name == "Form3") 53 { 54 b = false; 55 } 56 } 57 if (this.MdiChildren.Length == 0||b) 58 { 59 Form3 fm = new Form3(); 60 fm.MdiParent = this; 61 fm.Show(); 62 } 63 } 64 }
PS:
1.父窗体:IsMdiContainer=true
2.子窗体:建一个窗体,设计好。在父窗体“新建子窗体”界面中,把子窗体new出来。把新new出来的子窗体的MdiParent属性设置为父窗体对象。