C # Winform TabControl TabPage double-click to close

When using the TabControl control, we need to consider TabPage page automatically created - off function

A thought: Custom TabControl control, add a close button

Two ideas: TabControl control TabPage ToolTip display attributes (e.g., double click to close)

Here, explain ideas II implementation

Steps:

The first step: TabControl TabPage add multiple pages, ShowToolTip = true;

Step two: TabPage, ToolTipText = "Double-click close tab";

The third step: to achieve double-click events MouseDoubleClick, as follows:

 private void tabControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //TabPage page = sender as TabPage;
            //this.tabControl1.TabPages.Remove(page);
            tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
        }

 

Dynamically add a reference to the following TabPage

AddTabPage ( "DirectRegisterForm", "center registered");

 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;
            }
        }

  

  

  

 

Guess you like

Origin www.cnblogs.com/YYkun/p/11466743.html