Blue Theme Orange Theme Green Theme Red Theme
 
HeaderAd
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
New MS SQL 2008 Available - DiscountASP.NET
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Windows Controls » 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:
Technologies: .NET 1.0/1.1, Controls,Visual C# .NET
Total downloads :
Total page views :  146067
Rating :
 5/5
This article has been rated :  1 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
ArticleAd
Become a Sponsor



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;


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Mahesh Chand
Mahesh is a software consultant, architect, author, MCP, MVP, and founder of C# Corner. He has over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries including Microsoft, Unisys, Barclay’s, Centocor (J&J), McGraw-Hill, Excelon, PMI, and University of Pennsylvania Hospital. Since year 2000, he is been working with, ASP.NET, SQL Server, C# and .NET. His latest experience and interest is Silverlight, WPF, WCF, XAML and .NET 3.5. If you need any consulting in ASP.NET, AJAX, WPF, WCF, or XAML, contact him 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.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
How to disable TabsRavi1/30/2006
Please let me know How to disable Tabs?
Reply | Email | Delete | Modify | 
Disable Tab PagesFletcher2/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 | Delete | Modify | 
Migrating tabcontrol from .Net 1.1 to 2.0Spiti2/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 | Delete | Modify | 
Add controls to a programatically added controlHarry5/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 | Delete | Modify | 
navigating from one tab page to othermanju7/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 | Delete | Modify | 
 
 
Re: navigating from one tab page to otherMahesh7/12/2007
Did you try TabControlName.SelectedTab = TabPageYouWantToSelect;
Reply | Email | Delete | Modify | 
 
Re: Re: navigating from one tab page to othermanju7/13/2007

thanks mahesh

 

Reply | Email | Delete | Modify | 
How to select another tabPriscila8/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 | Delete | Modify | 
 
 
Re: How to select another tabshoaib9/4/2007

Try the below..

if( tabControl1.SelectedTab == tabPage1 )

{

//Ur code

}

if( tabControl1.SelectedIndex == 0 )

{

//Ur code

}

 

Reply | Email | Delete | Modify | 
Tab Alignmentshoaib9/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 | Delete | Modify | 
Tabcontrol - multiple code filesElna1/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 | Delete | Modify | 
 
 
Re: Tabcontrol - multiple code filesdexteraruns8/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 | Delete | Modify | 
Working with Web based TabControl with C#??chandru2/11/2008
How to use tab control in web based application + C#??
Reply | Email | Delete | Modify | 
 
 
Re: Working with Web based TabControl with C#??Mahesh4/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 | Delete | Modify | 
Tabcontrol with keyboardsrikanth11/21/2008
How to change tab pages with key board without using mouse in c# tab controls?
Reply | Email | Delete | Modify | 
How to Remove Tab controls from a Form Mark2/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 | Delete | Modify | 
 
 
Re: How to Remove Tab controls from a Form Mahesh4/7/2009

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

Reply | Email | Delete | Modify | 
How to access the Controls in Tab pagesVani3/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 | Delete | Modify | 
 
 
Re: How to access the Controls in Tab pagesMahesh4/2/2009
You need to post your code or post your question on C# Corner forums with the code sample.
Reply | Email | Delete | Modify | 
How to access the controls in Tab pagesVani3/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 | Delete | Modify | 
Not able to create new fileVani3/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 | Delete | Modify | 
 
 
Re: Not able to create new fileMahesh4/2/2009
Please post your questions on Forums. Click on Forums link on the header.
Reply | Email | Delete | Modify | 
Thank you.mike5/2/2009
Thank you for setting this site up. Your lessons are second to none. Every well explained !!
Reply | Email | Delete | Modify | 
Similar topic of how to activate a specific tab - but differentGeorge5/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 | Delete | Modify | 
what is the equivalent on web pages?Jose5/18/2009
Hi..
    can you please tell us what is the tab control equivalent for web forms?

thanks
Jose
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved