LIDOR SYSTEMS

Advanced User Interface Controls and Components

Create or Open New Tabs in TabControl .NET

Created: 23 September 2013

The most common way to add or open a new tab in TabControl is by using a Button which when clicked will add a new tab. However, we can make this easier if we add this option using an empty tab which is always shown at the end of all tabs, like in Google Chrome browser for example..

Open a New Tab in TabControl
Download Sample - Open New Tab in TabControl .NET

We can achieve this easily by simply adding an empty tab, that is a tab with empty title, and when it is clicked it will create a new tab and add it to the end of the tab list. Instead of an empty text, you can add a + (plus) image, just create an image and apply it to this tab Image or ImageIndex property.

To distinguish this tab from all other tabs we will apply a specific Tag to it, in this case a string “OPEN_NEW_TAB”. To handle clicks we will use the Click event of the TabControl like this:

private void tabControl1_Click(object sender, EventArgs e)

{

// Convert the mouse cursor location to local coordinates

Point mousePos = this.tabControl1.PointToClient(Control.MousePosition);

 

// Get the tab page which the mouse cursor points to

LidorSystems.IntegralUI.Containers.TabPage page = this.tabControl1.GetPageAt(mousePos);

if (page != null)

{

// If click is made over tab responsible for creating new tabs, open a new tab

if (page.Tag != null && page.Tag.ToString() == "OPEN_NEW_TAB")

{

int count = this.tabControl1.Pages.Count;

this.tabControl1.Pages.Insert(count - 1, "New Page");

}

}

}

Private Sub tabControl1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tabControl1.Click

' Convert the mouse cursor location to local coordinates

Dim mousePos As Point = Me.tabControl1.PointToClient(Control.MousePosition)

 

' Get the tab page which the mouse cursor points to

Dim page As LidorSystems.IntegralUI.Containers.TabPage = Me.tabControl1.GetPageAt(mousePos)

If page IsNot Nothing Then

' If click is made over tab responsible for creating new tabs, open a new tab

If page.Tag IsNot Nothing AndAlso page.Tag.ToString() = "OPEN_NEW_TAB" Then

Dim count As Integer = Me.tabControl1.Pages.Count

Me.tabControl1.Pages.Insert(count - 1, "New Page")

End If

End If

End Sub

The new tab page will be inserted just before our empty tab, in this way the empty tab will always appear at the end.

Also because the empty tab (the open new tab) is just like other tabs, we need to make sure it will not be closed whenever we try to remove some tab. To accomplish this we will handle the PageClosing event, and if we try to close this tab, we will cancel the process:

private void tabControl1_PageClosing(object sender, LidorSystems.IntegralUI.ObjectCancelEventArgs e)

{

if (e.Object is LidorSystems.IntegralUI.Containers.TabPage)

{

LidorSystems.IntegralUI.Containers.TabPage page = (LidorSystems.IntegralUI.Containers.TabPage)e.Object;

 

// Prevent from closing of the tab page which creates new tabs

if (page.Tag != null && page.Tag.ToString() == "OPEN_NEW_TAB")

e.Cancel = true;

}

}

Private Sub tabControl1_PageClosing(ByVal sender As Object, ByVal e As LidorSystems.IntegralUI.ObjectCancelEventArgs) Handles tabControl1.PageClosing

If TypeOf e.[Object] Is LidorSystems.IntegralUI.Containers.TabPage Then

Dim page As LidorSystems.IntegralUI.Containers.TabPage = DirectCast(e.[Object], LidorSystems.IntegralUI.Containers.TabPage)

 

' Prevent from closing of the tab page which creates new tabs

If page.Tag IsNot Nothing AndAlso page.Tag.ToString() = "OPEN_NEW_TAB" Then

e.Cancel = True

End If

End If

End Sub

A sample project in C# and VB which shows how to add or open a new tab using an tab at the end of tabs, can be downloaded from here: Open New Tab in TabControl

Newsletter


Sign-up to our newsletter and you will receive news on upcoming events, latest articles, samples and special offers.
Name: Email: *
*By checking this box, I agree to receive a newsletter from Lidor Systems in accordance with the Privacy Policy. I understand that I can unsubscribe from these communications at any time by clicking on the unsubscribe link in all emails.