Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms TabControl - Add New Tab Button (+)

How can I add a + button to the TabControl in a Windows Forms Application. Here is an answer for WPF. But I want it in WinForms application?

like image 616
clumpter Avatar asked Jul 18 '11 19:07

clumpter


2 Answers

You can add a new tab to the end of tabs of control and set it's text to + and then:

  • Check if the user clicked on the last tab, then insert a new tab before it.
  • You should prevent selection of the last tab.
  • You should adjust the width of tabs and let the last tab have smaller width.

Then you will have a tab control like below. To have larger tab buttons, I have applied a padding to the control.

Tab Control Add Button

Hanlde Click on Last Tab

You can handle MouseDown or MouseClick event and check if the last tab rectangle contains the mouse clicked point, then insert a tab before the last tab:

private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
    var lastIndex = this.tabControl1.TabCount - 1;
    if (this.tabControl1.GetTabRect(lastIndex).Contains(e.Location))
    {
        this.tabControl1.TabPages.Insert(lastIndex, "New Tab");
        this.tabControl1.SelectedIndex = lastIndex;
    }
}

Prevent Selectin of Last Tab

To prevent selection of last tab, you can handle Selecting event of control and check if the selecting tab is the last tab, cancel the event:

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
    if (e.TabPageIndex == this.tabControl1.TabCount - 1)
        e.Cancel = true;
}

Adjust Width of Tabs

To adjust tab width and let the last tab have smaller width, you can hanlde HandleCreated event and send a TCM_SETMINTABWIDTH to the control and specify the minimum size allowed for the tab width:

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private const int TCM_SETMINTABWIDTH = 0x1300 + 49;
private void tabControl1_HandleCreated(object sender, EventArgs e)
{
    SendMessage(this.tabControl1.Handle, TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)16);
}

Note

  • You can simply encapsulate the logic in a derived TabContol and make a custom tab control which supports adding tabs.

  • Close button: Also you can simply make the control owner-draw and handle Painting of tabs to show a + icon and X icon on tabs. As an example you can see an implementation in this post: TabControl with Close and Add Button.

  • Right to Left (RTL) support: You can add support for RTL when using owner-draw tab. This post: Close button for TabPages of Right To Left TabControl is a solution.

like image 192
Reza Aghaei Avatar answered Oct 02 '22 05:10

Reza Aghaei


I would add a new TabPage, then set the header to "+", set it's name to newTabPage and add an event for the TabControl's SelectedIndexChanged. Then you just check if

tabcontrol.SelectedTab == newTabPage 

and if that is the case you can create a new TabPage, insert it into tabControl and set it as the SelectedTab like:

tabControl.TabPages.Insert(tabControl.TabPages.Count - 1, createdTabPage);
tabControl.SelectedTab = createdTabPage;
like image 20
Fed44 Avatar answered Oct 02 '22 04:10

Fed44