Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » Windows Controls C# » Working with Windows TabControl with C#

Working with Windows TabControl with C#

Windows TabControl is a useful control that allows you display multiple dialogs tabs on a single form by switching between the tabs. In this tutorial, I will explain how to create and use a TabControl in your Windows applications with C#.

Author Rank :
Page Views : 361899
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Windows TabControl is a useful control that allows you display multiple dialogs tabs on a single form by switching between the tabs. A tab acts as another Form that can host other controls. Figure 1 shows an example of TabControl in Visual Studio .NET, which allows you to switch among multiple files using the tabs.

In this tutorial, I will explain how to create and use a TabControl in your Windows applications with C#.

Adding a TabControl to Form

The simplest way to create a TabControl is using Visual Studio .NET. I create a Windows Form application using Visual Studio .NET and add a TabControl from Toolbox to the Form by dragging the TabControl to the Form. After that I resize and reposition TabControl according to the Form size. The Form Designer adds the code for TabControl for you. If you see the code, you will notice once private variable of type System.Windows.Forms.TabControl as following:

private System.Windows.Forms.TabControl tabControl1;

The System.Windows.Forms.TabControl class represents a TabControl in .NET. Now if you see the InitializeComponent method generated by the Form Designer, you will see the code for TabControl such as setting TabControl location, name, size and adding the TabControl to the Form controls. See Listing 1.

private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Location = new System.Drawing.Point(8, 16);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(352, 248);
this.tabControl1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(368, 273);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}

 Figure 1 shows an example

Understanding the TabControl and TabPage class

A TabControl is a collection of tab pages and a tab page is the actual control that hosts other child controls. TabPage class represents a tab page.

TabControl class represents a TabControl. This class provides members (properties, methods, and events) to work with the TabControls. Table 1 lists the TabControl properties.

Property  Description
Alignment
Area of the control where the tabs are aligned.
Appearance
 
Visual appearance of the control's tabs.
DrawMode
 
A way that the control's tab pages are drawn.
HotTrack Value indicating whether the control's tabs change in appearance when the mouse passes over them.
ImageList
The images to display on the control's tabs.
ItemSize
 
Size of the control's tabs.
Multiline
 A value indicating whether more than one row of tabs can be displayed.
Padding
 
Amount of space around each item on the control's tab pages. 
RowCount
 
Returns the number of rows that are currently being displayed in the control's tab strip.
SelectedIndex
 
The index of the currently-selected tab page.
SelectedTab  Currently selected tab page.
ShowToolTips
 
The value indicating whether a tab's ToolTip is shown when the mouse passes over the tab.
SizeMode
 
The way that the control's tabs are sized.
TabCount
 Number of tabs in the tab strip.
TabPages
Returns the collection of tab pages in this tab control.

Adding TabPage to a TabControl

Now I will add few tabs to the TabControl with the help of Properties window of TabControl. The Properties window has a property called TabPages, which is a collection of TabPage controls (see Figure 2). A TabPage represents a page of the TabControl that can host child controls.

Figure 2. TabPages property of TabControl

Now if you click on TabPages property in Property window, it launches TabPage Collection Editor (see Figure 3) where you can add a new page or remove existing pages by using Add and Remove buttons. You can also set the properties of pages by using the right side properties grid. As you can see from Figure 3, I add two pages and set their properties.



Figure 3. Adding Tab pages to a TabControl

After adding two pages to TabControl, the final Form looks like Figure 4.

Figure 4. A Form with two Tab pages

Adding and Removing a TabPage to TabControl Programmatically

You can add and remove Tab pages to a TabControl using the TabControl.TabPages.Add and TabControl.TabPages.Remove methods. The following code snippet adds a new page to the TabControl programmatically:

TabPage newPage = new TabPage("New Page");
tabControl1.TabPages.Add(newPage);

After adding the page, the new TabControl would look like Figure 5.

Figure 5. Adding a Tab page programmatically.

The Remove method of TabPageCollection class (through TabControl.TabPages) removes a page by name or index from the page collection. The following code snippet removes "New Page" from the collection:

TabPage newPage = new TabPage("New Page");
tabControl1.TabPages.Remove(newPage);

The RemoveAll method removes all the pages from the collection.

Adding Controls to a TabPage

Adding controls to a TabPage is similar to adding controls to a Form. Make a page active in the Form Designer and drag and drop controls from Toolbox to the page. I add a Label, a TextBox, and a Button control to Settings page of TabControl and change their properties. The final page looks like Figure 6.


Figure 6. Adding controls to a Tab page

Controls are added to a page by using TabPage.Controls.Add method. Now if you see the code generated by the designer, you will notice the following code:

this.SettingsPage.Controls.Add(this.BrowseBtn);
this.SettingsPage.Controls.Add(this.textBox1);
this.SettingsPage.Controls.Add(this.label1);

Using the same code, you can even add controls to a TabPage programmatically.

Access Controls of a TabPage

All controls of a TabPage are local to a Form and accessible from the Form without adding any additional functionality. For example, the following code sets the Text property of the TextBox on Preferences Tab page:

this.textBox1 =@"C:\";

Getting and Setting Active Tab Programmaticall

You can get and set an active tab of a TabControl programmatically using the SelectedTab property of TabControl. For example, the following code snippet sets PreferencePage as active tab:

this.tabControl1.SelectedTab = this.PreferencesPage;

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by amar on Jan 25, 2012
Contents added by Mark McGinty on Feb 04, 2011
Is there a way to make the TabControl at runtime look more like it does at design time in VS2010?  When designing, the non-selected tabs are shaded a darker color than is the selected one; also, when you roll over a non-selected tab, its background turns a light shade of blue -- not necessarily the color I would chose, but a worthy affect.

My problem with its runtime looks out of the box is that it's not clear enough which tab is selected, a problem compounded if more than one tab has the same layout, each tab pertaining to a different logical object.  I realize I could go owner draw, but that's rather a lot of work to do something that has clearly already been done in the designer UI (that is equally clearly specific to this component.)

Any help?

TIA,
MM
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
How to disable Tabs by Ravi On January 30, 2006
Please let me know How to disable Tabs?
Reply | Email | Modify 
Disable Tab Pages by Fletcher On February 20, 2007
As you've no doubt seen, the visible property has no meaning to a tab page. If you wish to disable a page, create an event handler for the "Selecting" event. This event is fired when a user clicks on a new tab. You can identify the new tab with e.TabPage as so: switch(e.TabPage.TabIndex) //gives you the index of the //tab that was clicked { case 0: //do something break; case 2: //we don't want this one to be selected, so we cancel it e.Cancel = true; break; } Now, if you want to make a tab page disappear completely, you have to physically remove it from the TabPages collection in the tabControl control: tabControl1->TabPages->Remove(tabPage2); Adding it back is just as easy, using the Add() or Insert() method instead of the Remove() method. Choose the one that best suits your needs. Here, I'm using Insert so that I can specify where in the list I wish to insert the tab page: tabControl1->TabPages->Insert(1,tabPage2); Hope This helps!
Reply | Email | Modify 
Re: Disable Tab Pages by David On November 8, 2010

I'm trying to use switch(e.TabPageIndex) inside my "Selected" event handler, but want to make sure that if the tab pages are rearranged, my code will continue to work properly. I've tried using tabControl.TabPages.IndexOf(tabPage), but get an error of "A constant value is expected.".

Do you have any ideas on how to better check which page is selected, and then do something different based on the selected page?

Thank you

Reply | Email | Modify 
Migrating tabcontrol from .Net 1.1 to 2.0 by Spiti On February 28, 2007
I have piece of code in which the tab pages are set progrmatically using selectedtab property. This should trigger selctedIndexchanged event. I have an event handler to handle this event. In 2.0 this event does not seem to be triggered! whereas in 1.1 it is working fine. Any thoughts?
Reply | Email | Modify 
Add controls to a programatically added control by Harry On May 14, 2007
i have set my software up to add a tab, but i want it to add some controls to it. The tab is automatically named "TabPage" then +1 to the Tabcount. so if there is 5 tabs already and u add a new 1, it becomes TabPage6. Now how do i add controls to the page that has automatically been added?
Reply | Email | Modify 
navigating from one tab page to other by manju On July 12, 2007
I have a tabpage holding a button,on click of this button i want to open another tabpage. please help me out in doing this
Reply | Email | Modify 
Re: navigating from one tab page to other by Mahesh On July 12, 2007
Did you try TabControlName.SelectedTab = TabPageYouWantToSelect;
Reply | Email | Modify 
Re: Re: navigating from one tab page to other by manju On July 13, 2007

thanks mahesh

 

Reply | Email | Modify 
How to select another tab by Priscila On August 22, 2007
I would kike to select an another tab for me to write code. For example, I need to write as this: if (tabSelected == tab1) ...... else if (tabSelected == tab2) ..... Question: What to put in "tabSelected"
Reply | Email | Modify 
Re: How to select another tab by shoaib On September 4, 2007

Try the below..

if( tabControl1.SelectedTab == tabPage1 )

{

//Ur code

}

if( tabControl1.SelectedIndex == 0 )

{

//Ur code

}

 

Reply | Email | Modify 
Tab Alignment by shoaib On September 4, 2007
When i change the alignment of tab control to left/right, the text in the tab pages are not seen at run time also. Am i missing something??
Reply | Email | Modify 
Tabcontrol - multiple code files by Elna On January 21, 2008
I have used a tabcontrol with 8 tabsheets in a project and the problem is that the file containing the eventhandlers for all the controls on the different tabsheets as well as the functions I have written, is very big, almost 6000 lines of code. Is there a way that I can create different code files so that the code for each tabsheet (event handlers and funcions) is in a different file?
Reply | Email | Modify 
Re: Tabcontrol - multiple code files by dexteraruns On August 29, 2008
Tried using partial classes? It lets you have a file for each partial class as long as they're all under the same namespace.
Reply | Email | Modify 
Working with Web based TabControl with C#?? by chandru On February 11, 2008
How to use tab control in web based application + C#??
Reply | Email | Modify 
Re: Working with Web based TabControl with C#?? by Mahesh On April 7, 2009
Search this site for Tab control in ASP.NET. Here is one article I found:

Extending ASP.NET 2.0 Menu Control To Have Tabs With Rounded Corners by Mike Clark on Feb 26, 2007
The ASP.NET Menu Control normally produces tabs which have rectangular edges. The techniques presented extend the control to create tabs with rounded corners.
Reply | Email | Modify 
Tabcontrol with keyboard by srikanth On November 21, 2008
How to change tab pages with key board without using mouse in c# tab controls?
Reply | Email | Modify 
How to Remove Tab controls from a Form by Mark On February 2, 2009
I am converting an application written for windows pocket pc (windows Mobile) that supports tab controls to a smartphone application. The smartphone does not support tab controls. Wondering if there is some way to remove the tab controls and get all of the form fields on one scrollable page. Not sure this is doable or not. Thanks !
Reply | Email | Modify 
Re: How to Remove Tab controls from a Form by Mahesh On April 7, 2009

I have not done much smart app development. You may want to post your question on forums.

Reply | Email | Modify 
How to access the Controls in Tab pages by Vani On March 30, 2009
My application involves adding the control RichTextBox to the tab page in one method and accessing this RichTextBox that is the control in the TabPage in some other method. I am trying it as tabControl1.SelectedTab.Controls(0).Text But its Not working.this function works when used in VB.Net...please let me know where I am doing it Wrong...
Reply | Email | Modify 
Re: How to access the Controls in Tab pages by Mahesh On April 2, 2009
You need to post your code or post your question on C# Corner forums with the code sample.
Reply | Email | Modify 
How to access the controls in Tab pages by Vani On March 30, 2009
My application involves adding the control RichTextBox to the tab page in one method and accessing this RichTextBox that is the control in the TabPage in some other method. I am trying it as "tabControl1.SelectedTab.Controls(0).Text". But its Not working.this function works when used in VB.Net...please let me know where I am doing it Wrong...
Reply | Email | Modify 
Not able to create new file by Vani On March 30, 2009
while doing File handling I am not able to create a new file..the code i am using is " FileStream fs=new FileStream(openFileDialog1.FileName, FileMode.Create, FileAccess.ReadWrite); " But its opening the existing File only..When i try to create a new file.It prints a message .file path is not correct. i dont know where i creating a mistake. Please clarify my doubts
Reply | Email | Modify 
Re: Not able to create new file by Mahesh On April 2, 2009
Please post your questions on Forums. Click on Forums link on the header.
Reply | Email | Modify 
Thank you. by mike On May 2, 2009
Thank you for setting this site up. Your lessons are second to none. Every well explained !!
Reply | Email | Modify 
Similar topic of how to activate a specific tab - but different by George On May 4, 2009
I'm building an Outlook FormRegion with a tabControl, with about 40 tabs (Outlook 2007, VSTO 2008, and I'll probably "hide" a bunch of them in production).  As a result of a button click on one tab I need to switch to another tab.  This didn't seem to be such an obstacle until every solution I tried resulted in a "... does not exist in the current context."  That context being the button_Click. 

You seen to know alot about this control so I thought I might trouble you for any insights you might have.

Regards,
Reply | Email | Modify 
what is the equivalent on web pages? by Jose On May 18, 2009
Hi..
    can you please tell us what is the tab control equivalent for web forms?

thanks
Jose
Reply | Email | Modify 
tab control on smart device by veera On February 19, 2010
how to use tab control on top in smart device

Reply | Email | Modify 
how to jump to another tabpage from one tabpage ?? by thiam On April 15, 2010
hi,how to jump to another tabpage from a tabpage by click a button?? thanks
Reply | Email | Modify 
C#Net2008 Navigate TABCONTROL by Lennie On June 15, 2010
Hi there,
How to switch on to other TABPAGE when the TABCONTROL TABPAGE button is clicked on ?

This coding is not working:

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)

{

Select Case true

{

Case tabControl1.SelectedTab is this.tabPageDisplay;

tabControl1.SelectedTab = this.tabPageDisplay;

Case tabControl1.SelectedTab is this.tabEditSales;

tabControl1.SelectedTab = this.tabEditSales;

}

}

Reply | Email | Modify 
thanks by vimal On September 14, 2010
hi Mahesh Chand 
  how to call manualy the tabpages without clicking the tabbar?
Reply | Email | Modify 
Re: thanks by Mahesh On September 14, 2010
Just change the SelectedTab to the tab you want to make active.  

this.tabControl1.SelectedTab = this.PreferencesPage;

Reply | Email | Modify 
scrollbar problem by kaliappan On October 7, 2010
Is there a way to fix the scrollbar problem when you open a form with a tab control on it. Seems it wants to scroll down and hide the tabs. Any suggestions?
Reply | Email | Modify 
Re: scrollbar problem by David On November 8, 2010
Have you checked which control has focus? It may be that you're setting focus to a control in the tabpage or a control below the tabpage, which is then causing the scroll-bar to automatically scroll so that control is visible, causing the tabs to no longer show.

Just a thought, not sure how useful it will be.
Reply | Email | Modify 
Re: scrollbar problem by Mahesh On November 8, 2010
Can you upload your screen shot here?
Reply | Email | Modify 
Adding a scrollbar for right-aligned tabs in Windows Forms C# .NET by Sid On November 16, 2010
I'm trying to add a scrollbar for right-aligned tabs so the tabs don't start stacking in another line after they fill one line.
If the tabs are top or bottom aligned I can set multiline to false and I get the scroll buttons, but this doesn't work for right-aligned tabs.
Any suggestions or help would be greatly appreciated :)
Thanks
Reply | Email | Modify 
Question by Pavan On January 18, 2011
Sir, This document help me a lot. I want to use several tab in tabs. Is it possible?
Reply | Email | Modify 
Want to Disable tabpage from 2nd form by shahzad On January 20, 2011
i have 2 forms. 1- having tabcnotrol and pages in it and all are disabled programmatically at tab's selectedindexchanged event. this is the code (this.tbpItemCorrection as Control).Enabled = false; now i want to enable the tabpage from 2nd form by clicking the button. what i have done so for is this i made all form one and tbacontrol,pages public and used the following code (this.tbpItemCorrection as Control).Enabled = true; but it is not enabling tabpage. Help me please
Reply | Email | Modify 
Working With Menu And Tab Control by deepu On May 10, 2011
hai.. i want to nagivate to particular tab from menu.I can provide more information. Suppose in Menu i have Add Manage View If the user click on the add,it should navigate to tabpage1,if manage then to tabPage2 else tabPage3. How can i get this?Please Help ME
Reply | Email | Modify 
Re: Working With Menu And Tab Control by Mahesh On May 10, 2011
Just look at the SelectedTab property and whatever tab you want to select, make that selected tab. I am assuming you already know the name of the tabs.
Reply | Email | Modify 
Re: Re: Working With Menu And Tab Control by deepu On May 12, 2011
yes.i know about selected tab property.but how can i set like if user clicked on add menu then show tabpage1 ,if manage then tabpage 2.i am using MDI forms.from that form i want to redirect to a form where i added tab control based on selected item.how can i do this?
Reply | Email | Modify 
Tabcontrol C# code by Tom On November 2, 2011
Hi Mahesh I started a new C# Windows Forms application (using Visual C# 2010 Express) and added a TabControl to the form. But my code is very different to yours. My code is: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Tabtest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } } } Why is it so different to yours? Am I missing something? Tom
Reply | Email | Modify 
Re: Tabcontrol C# code by Mahesh On November 23, 2011
What version of Visual Studio are you using? This was written in year 2003 :)
Reply | Email | Modify 
Re: Re: Tabcontrol C# code by Tom On November 23, 2011
It's Visual C# Express 2010 tom
Reply | Email | Modify 
Re: Re: Re: Tabcontrol C# code by Sivaraman On November 24, 2011
Tom! So the default is changed from 2003 Ide to 2010 IDE. Do not worry about it. Those includes (or Using statements) are to get access to the Microsoft library.
Reply | Email | Modify 
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.