1

I want to add a close button to header of my TabPages. So, like browser tabs it can be removed from the TabControl. Thank you in advance for your time.

EDIT :

Now, I'm handling the closing with middle click like the code below but I want to provide my user with a friendly close button on the tabPage header.

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

Edit

I found out that my question is repeated and correctly have answered there.

Community
  • 1
  • 1
Mohammad Chamanpara
  • 2,049
  • 1
  • 15
  • 23
  • What have you tried so far? Which aspect of this are you having trouble with (is it adding a button, removing the tab from the TabPages collection or something else)? – Bob Sammers Aug 05 '15 at 16:59
  • @BobSammers I edited the question. that's what I've done in order to reach that goal. My problem is adding the close button. – Mohammad Chamanpara Aug 05 '15 at 17:03
  • See [here](http://stackoverflow.com/questions/26315025/how-to-add-and-remove-custom-tabs-in-c-sharp/26315325?s=2|0.2579#26315325) for an example – TaW Aug 05 '15 at 18:08
  • That's right my friend. It's a duplicate question. The onus is on me to search more before asking a question. please show me how to remove it. @Hans Passant – Mohammad Chamanpara Aug 07 '15 at 07:53

2 Answers2

0

The default WF TabControl does not have this functionality.

You could have another button on your UI, with that you could remove the current tab.

If it is required to have the button on the tab, you could override "OnPaint" method and draw it by you own or use antoher tab control.

There are also free tab controls:

A TabControl with tab page closing capability

Firefox-like subclassed TabControl and TabPage

CodeTherapist
  • 2,776
  • 14
  • 24
0

You may add a close button on every tab by calling this procedure (not tested):

private List<Button> closeButtons = new List<Button>()

private void SetTabButtons()
{
  // first remove all existing buttons
  for (int i=0;i<closeButtons.Count;i++) closeButtons[i].Parent=null ;
  closeButtons.Clear() ; 
  // create the close buttons
  for (int i=0;i<theTabControl.TabPages.Count;i++) 
  {
    // add some spaces to tab text for the close button
    theTabControl.TabPages[i].Text = theTabControl.TabPages[i].Text+"     "  ;
    Rectangle rect=theTabControl.GetTabRect(i);
          NewControl.Location = new System.Drawing.Point(X, Y);
    Button b = new Button() ;
    button.Text = "x" ;
    button.AutoSize = true;
    button.Location = new Point(rect.Right-button.Width-3,rect.top+3) ; 
    button.Parent   = theTabControl ;
    button.tag      = i ;
    button.Click   +=CloseTab_ButtonClick ;
    closeButtons.Add(button) ;
  }

  private void CloseTab_ButtonClick(object sender, EventArgs e) 
  { 
    int theTabPageIndex = (int)((Button)sender).Tag) ;
    // remove the tabpage here 
    ...
    // reset the buttons
    setTabButtons() ;
  }
Graffito
  • 1,658
  • 1
  • 11
  • 10