在使用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; } }