开发者

WinForms TabControl - Add New Tab Button (+)

开发者 https://www.devze.com 2023-03-20 22:56 出处:网络
How can I add a + button to the TabControl in a Windows Forms Application. Here 开发者_如何学Cis an answer for WPF. But I want it in WinForms application?You can add a new tab to the end of tabs of co

How can I add a + button to the TabControl in a Windows Forms Application. Here 开发者_如何学Cis an answer for WPF. But I want it in WinForms application?


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.

WinForms TabControl - Add New Tab 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.


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;


Create a tab with tag "+" , and use "tabControl1_Selecting" event, and the code this:

if(e.TabPageIndex==tabControl1.TabPages.Count-1)
    tabControl1.TabPages.Insert(tabControl1.TabPages.Count - 1,"tab"+e.TabPageIndex);


Some improvements according to @Reza Aghaei

  1. Create a new form and name it formTab(set the form boarder style to none).
  2. Set the controls of formTab (this will be the controls set in the new tab).
  3. go back to the tabcontrol and add a new tab, name it "+"(make sure that this tab is the end of tabcontrol).

your form should look some what like these: form1 formTab

now type in these codes:

private void Form1_Load(object sender, EventArgs e)
    {
        TabPage tab = tabControl1.TabPages[0];
        var newTab = new formTab();
        newTab.TopLevel = false;
        newTab.Dock = DockStyle.Fill;
        newTab.Show();
        newTab.Visible = true;
        tab.Controls.Add(newTab);
    }


private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {
        var tabAdd = tabControl1.TabCount - 1;

        if (tabControl1.SelectedIndex == tabAdd)
        {
            //create a new tabpage
            var t = new TabPage();

            //create a new formTab with webControl in it
            var newTab = new formTab();

            //show the new formTab
            newTab.Show();
            newTab.TopLevel = false;
            newTab.Dock = DockStyle.Fill;
            newTab.Visible = true;

            //add formTab as new control in the tabpage just created
            t.Controls.Add(newTab);

            //insert the new created tab into tab control and before tabLoc
            tabControl1.TabPages.Insert(tabAdd, t);

            //select the new created tab
            var newCreatedTab = tabControl1.TabCount - 2;
            tabControl1.SelectedIndex = newCreatedTab;
        }
    }

hope it helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号