// new/close tab with key bindings public void MasterTabControl_KeyDown(object sender, KeyEventArgs e) { tabControl2 webControl = new tabControl2(); webControl.Dock = DockStyle.Fill; TabPage tp = new TabPage(); tp.Text = "New Tab"; tp.Controls.Add(webControl); if (e.Control && (e.KeyCode == Keys.T)) //opens new tab { this.MasterTabControl.TabPages.Add(tp); this.MasterTabControl.SelectTab(tp); this.MasterTabControl.Focus(); // keeps tab control focused so we can add/close more tabs } if (e.Control && (e.KeyCode == Keys.W)) //closes tab { if (MasterTabControl.TabPages.Count > 1) { var tabIndex = this.MasterTabControl.SelectedIndex; this.MasterTabControl.TabPages.Remove(MasterTabControl.SelectedTab); if (tabIndex >= 1) // logic to prevent trying to close more tabs than exist { this.MasterTabControl.SelectTab(tabIndex - 1); } else { this.MasterTabControl.SelectTab(tabIndex); } this.MasterTabControl.Focus(); } } }
//NEW TAB FROM MENU private void newTabToolStripMenuItem_Click(object sender, EventArgs e) { tabControl2 webControl = new tabControl2(); webControl.Dock = DockStyle.Fill; TabPage tp = new TabPage(); tp.Text = "New Tab"; tp.Controls.Add(webControl); this.MasterTabControl.TabPages.Add(tp); this.MasterTabControl.SelectTab(tp); this.MasterTabControl.Focus(); }