Customizing a TabControl for the Closing of Individual Tabs

限于喜欢 提交于 2019-11-28 00:42:11
Daniel Brückner

I created a derived tab control about one year ago. I am not going to post the source here, because it's about 700 lines long and coded quite messy. Maybe I will find some time to clean the code up and then release it here. For now I will briefly outline the way it is build.

Each tab page has a 'X' icon to the left of the title and the tab pages support reordering by drag and drop and moving them between multiple tab control.

I choose the easy way to get the icon on the tab pages. The tab control has the TabControl.ImageList property and a tab page has a TabPage.ImageIndex property. So I just added three icons to a image list - normal, hover, pressed - and process the mouse events.

With TabControl.GetTabRect() you can test if the mouse is over a specific tab pages and with some math you find if it is over the icon. Then you just need to change the icon depending on the mouse button state and eventually remove the tab page under the mouse if the button was pressed.

The main problem with this solution is, that calculating if the mouse is over the icon requires to know where the icon is painted relative to the tab page and this might change with a new windows version. And the icon is to the left of the title, but that does not look too bad.

I found this code and was very helpful to me:

private void tabControl_MouseUp(object sender, MouseEventArgs e)
{
    // check if the right mouse button was pressed
    if(e.Button == MouseButtons.Right)
    {
        // iterate through all the tab pages
        for(int i = 0; i < tabControl1.TabCount; i++)
        {
            // get their rectangle area and check if it contains the mouse cursor
            Rectangle r = tabControl1.GetTabRect(i);
            if (r.Contains(e.Location))
            {
                // show the context menu here
                System.Diagnostics.Debug.WriteLine("TabPressed: " + i);
             }
        }
    }
}

TabControl: How To Capture Mouse Right-Click On Tab

Muhammad Hamad

I did the following: on the create (add) TabPage stage, I added a toolStrip

ToolStrip ts = new ToolStrip();
ts.Dock = DockStyle.Top;
ts.RightToLeft = System.Windows.Forms.RightToLeft.Yes;

Then, create the X button and add it to toolstrip

ToolStripButton ToolStripButton = new ToolStripButton("X");
ts.Items.Add(ToolStripButton);

create an event on clicking the X button

ToolStripButton.Click += new EventHandler(ToolStripButton_Click);

add toolstrip to the tabpage

tabControl1.TabPages[curenttabpage].Controls.Add(ts);

now for the ToolStripButton_Click is as follows:

void ToolStripButton_Click(object sender, EventArgs e)
{ 
 ToolStripButton t = (ToolStripButton)(sender);
 ToolStrip ts = t.Owner;
 TabPage tb = (TabPage)
 (ts.Parent);tabControl1.TabPages.Remove(tb);
}

Maybe it is not as you want, but it will work well.

I created a setup that is similar.

Each control that is added to the tab page at runtime is derived from a special base control I created. This base control has a close button (along with some other features such as safe to close flag).

Close tab code I'm using on my base control:

 TabPage tabpage = (TabPage)this.Parent;
TabControl tabControl = (TabControl)tabpage.Parent;

tabControl.TabPages.Remove(parent);

I know this is an old thread but I did find this link that will allow you to "hide" tabs in an array and then you can just re-load the tabs you want at run time. I added this more for a place I can easily find it again.

This code might help throgh closing the tab controls with middle mouse click :

            private void tabControl1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button != System.Windows.Forms.MouseButtons.Middle)
                    return;

                for (int i = 0; i < MainTabControl.TabPages.Count; i++)
                {
                    if (this.MainTabControl.GetTabRect(i).Contains(e.Location))
                    {
                        this.MainTabControl.TabPages.RemoveAt(i);
                        return;
                    }
                }
            }
Eric

It´s works!

TabPage tabpage = (TabPage)this.Parent;
TabControl tabControl = (TabControl)tabpage.Parent;
tabControl.TabPages.Remove(tabpage);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!