- Winform 使用NotNetBar
namespace WindowsFormExample { public partial class FrmMain : Office2007Form { public FrmMain() { InitializeComponent(); } private int index = 0; private void MenuItem(object sender, EventArgs e) { string menuItemTxt = ((DevComponents.DotNetBar.BaseItem)(sender)).Text; Office2007Form f = new Office2007Form(); switch (menuItemTxt) { case "File_ReadWrite": f = new Frm.FileExample(); break; case "DataGridExample": f = new Frm.FrmDataGrid(); break; case "1": break; case "2": break; case "3": break; case "4": break; case "5": break; default: break; } //for (int i = 0; i < this.tabControl1.Tabs.Count; i++) //{//已经打开了 就默认选中即可了 // if (tabControl1.Tabs[i].Text == menuItemTxt) // { // this.tabControl1.SelectedTab = tabControl1.Tabs[i]; // return; // } //} //f.TopLevel = false; //f.FormBorderStyle = FormBorderStyle.None; //f.WindowState = FormWindowState.Maximized; //TabPage Page = new TabPage(); //Page.Name = "Page" + index.ToString(); //Page.Text = menuItemTxt.ToString(); //Page.TabIndex = index; //// tabControl1.TabPages[0].Controls.Add(f); //Page.Controls.Add(f); //this.tabControl1.Controls.Add(Page); //f.TopLevel = false; //f.FormBorderStyle = FormBorderStyle.None; //f.WindowState = FormWindowState.Maximized; //TabItem it = new TabItem(); //it.Text = menuItemTxt.ToString(); //it.Name = "Page" + index.ToString(); //TabPage p = new TabPage(); //p.Container.Add(); //this.tabControl1.Tabs.Add(it); //#region 三种设置某个选项卡为当前选项卡的方法 ////this.tabControl1.SelectedIndex = index; ////this.tabControl1.SelectTab("Page" + index.ToString()); //#endregion //this.tabControl1.SelectedTabIndex = index; //f.Show(); //index++; AddForm(f); } private void AddForm(Office2007Form frm) { this.tabControl1.Visible = true; if (!IsOpenTab(frm.Text)) { TabItem tabItem = tabControl1.CreateTab(frm.Text); TabControlPanel tcp = new TabControlPanel(); tabItem.AttachedControl = tcp; tcp.TabItem = tabItem; tcp.Dock = DockStyle.Fill; tcp.Name = frm.Name + "Tab"; frm.TopLevel = false; frm.Dock = DockStyle.Fill; frm.FormBorderStyle = FormBorderStyle.None; frm.Show(); tcp.Controls.Add(frm); tabControl1.Controls.Add(tcp); tabControl1.SelectedTab = tabItem; } } /// <summary> /// 检查是否已打开某个子窗体 ///</summary> /// <param name="tabName"></param> /// <returns></returns> private bool IsOpenTab(string tabName) { bool isOpened = false; foreach (TabItem tab in tabControl1.Tabs) { if (tab.Text.Trim() == tabName.Trim()) { isOpened = true; tabControl1.SelectedTab = tab; break; } } return isOpened; } private void FrmMain_Load(object sender, EventArgs e) { } private void tabControl1_TabItemClose(object sender, TabStripActionEventArgs e) { this.tabControl1.Tabs.RemoveAt(this.tabControl1.SelectedTabIndex); if (tabControl1.Tabs.Count == 0) { AddForm(new Frm.FrmFirstPage()); } } } }