在使用TabControl控件时,需要考虑自动创建的TabPage页实现——关闭功能
思路一:自定义TabControl控件,添加关闭按钮
思路二:TabControl控件TabPage显示ToolTip属性(例如双击关闭)
此处,解释思路二,实现方式
操作步骤:
第一步:TabControl添加多个TabPage页,ShowToolTip=true;
第二步:TabPage,ToolTipText="双击关闭页签";
第三步:实现双击事件MouseDoubleClick,如下所示:
private void tabControl1_MouseDoubleClick(object sender, MouseEventArgs e) { //TabPage page = sender as TabPage; //this.tabControl1.TabPages.Remove(page); tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex); }
动态添加TabPage参考如下
AddTabPage("DirectRegisterForm", "登记中心");
private void AddTabPage(string formName, string formText) { TabPage page = this.tabContent.TabPages[formName]; if (page != null) { tabContent.SelectedTab = page; page.Select(); return; } TabPage tab = new TabPage(); tab.Name = formName; tab.Text = formText; Form form = GetForm(formName); form.TopLevel = false; form.FormBorderStyle = FormBorderStyle.None; form.Dock = DockStyle.Fill; tab.Controls.Add(form); tabContent.TabPages.Add(tab); tabContent.SelectedTab = tab; form.Show(); }
private Form GetForm(object itemName) { try { this.Cursor = Cursors.WaitCursor; Assembly assembly = Assembly.GetExecutingAssembly(); var path = "ZB.QueueSys.View." + itemName.ToString(); Form form = assembly.CreateInstance(path) as Form; return form; } catch { return null; } finally { this.Cursor = Cursors.Default; } }