SIGN UP MEMBER LOGIN:    
ARTICLE

C# TabControl

Posted by Mahesh Chand Articles | Windows Forms C# April 03, 2011
The TabControl manages tab pages where each page may host different child controls. In this article, I will demonstrate how to create and use a TabControl in Windows Forms using C#.
Reader Level:
Download Files:
 

TabControl Control

The TabControl manages tab pages where each page may host different child controls. In this article, I will demonstrate how to create and use a TabControl in Windows Forms.

I wrote an article Working with Windows TabControl with C#  in 2003 using .NET 2.0 and since then, things have been changed. This article is written using Visual Studio 2010.

Creating a TabControl

We can create a TabControl control using a Forms designer at design-time or using the TabControl class in code at run-time or dynamically.

Design-time

To create a TabControl control at design-time, you simply drag and drop a TabControl control from Toolbox onto a Form in Visual Studio. After you drag and drop a TabControl on a Form, the TabControl1 is added to the Form and looks like Figure 1.

TabControl1.gif

Figure 1

A TabControl is just a container and has no value without tab pages. As you can see from Figure 1, by default two Tab Pages are added to the TabControl. We can add and remove tab pages by clicking on the Tasks handle and selecting Add and Remove Tab links as you see in Figure 2.

TabControl2.gif

Figure 2

Add Tab link adds next tab page and Remove Tab removes the current tab page from a Tab Control. We will discuss tab pages in more details later in this tutorial.

Run-time

TabControl class represents a tab control. The following code snippet creates a TabControl and sets its Name, BackColor, ForeColor, Font, Width, and Height properties.

C# Code:

// Create a TabControl and set its properties

TabControl dynamicTabControl = new TabControl();
dynamicTabControl.Name = "DynamicTabControl";
dynamicTabControl.BackColor = Color.White;
dynamicTabControl.ForeColor = Color.Black;
dynamicTabControl.Font = new Font("Georgia", 16);
dynamicTabControl.Width = 300;
dynamicTabControl.Height = 200;


Once the TabControl control is ready with its properties, we need to add it to a Form by calling Form.Controls.Add method. The following code snippet adds a TabControl control to the current Form. 

C# Code:

Controls.Add(dynamicTabControl);

As I mentioned earlier, a TabControl is nothing without tab pages.

TabPage class represents a tab page control in Windows Forms. The following code snippet creates two TabPage controls, sets their properties and calls TabControl.TabPages.Add() method to add tab pages to TabControl.

C# Code:

// Add TabPage1

TabPage tabPage1 = new TabPage();
tabPage1.Name = "tabPage2";
tabPage1.Text = "Author";
tabPage1.BackColor = Color.Green;
tabPage1.ForeColor = Color.White;
tabPage1.Font = new Font("Verdana", 12);
tabPage1.Width = 100;
tabPage1.Height = 100;
dynamicTabControl.TabPages.Add(tabPage1);

// Add TabPage2

TabPage tabPage2 = new TabPage();
tabPage2.Name = "tabPage2";
tabPage2.Text = "Books";
tabPage2.BackColor = Color.Orange;
tabPage2.ForeColor = Color.White;
tabPage2.Font = new Font("Verdana", 12);
tabPage2.Width = 100;
tabPage2.Height = 100;
dynamicTabControl.TabPages.Add(tabPage2);


New TabControl with two tab pages looks like Figure 3.

TabControl3.gif

Figure 3

TabControl Properties

After you place a TabControl control on a Form, the next step is to set properties.

The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right click on a control and select Properties menu item. The Properties window looks like Figure 4.

TabControl4.gif

Figure 4

Name

Name property represents a unique name of a TabControl control. It is used to access the control in the code. The following code snippet sets and gets the name and text of a TabControl control.

C# Code:

dynamicTabControl.Name = "DynamicTabControl";


Positioning a TabControl

The Dock property is used to set the position of a TabControl. It is of type DockStyle that can have values Top, Bottom, Left, Right, and Fill. The following code snippet sets Location, Width, and Height properties of a TabControl control.

C# Code:

dynamicTabControl.Dock = DockStyle.Left;


Font

Font property represents the font of text of a TabControl control. If you click on the Font property in Properties window, you will see Font name, size and other font options. The following code snippet sets Font property at run-time.

C# Code:

dynamicTabControl.Font = new Font("Georgia", 16);


Background and Foreground

BackColor and ForeColor properties are used to set background and foreground color of a TabControl respectively. If you click on these properties in Properties window, the Color Dialog pops up.

Alternatively, you can set background and foreground colors at run-time. The following code snippet sets BackColor and ForeColor properties.

C# Code:

dynamicTabControl.BackColor = Color.White;
dynamicTabControl.ForeColor = Color.Black;


Alignment and Appearance

Alignment property gets or sets the area of the control where the tabs are aligned. A TabAlignment enumeration is used to set Alignment property and have Top, Bottom, Left, or Right values.

Appearance property gets or sets the visual appearance of the control's tabs. A TabAppearance enumeration is used to set the Appearance property and have Normal, Buttons, or FlatButtons values. 

C# Code:

dynamicTabControl.Alignment = TabAlignment.Left;
dynamicTabControl.Appearance = TabAppearance.FlatButtons;

Tab Pages

TabPages property, a type of TabPageColleciton object is the gateway to access and add tab pages to a TabControl. Like any other collections, TabPageCollection has all collection functionality including add, remove, and find.

We can add and access tab pages of TabControl at design-time from Properties Window by clicking on TabPages Collection as you can see in Figure 5.

TabControl5.gif

Figure 5

When you click on the Collections, the Tab Pages Collection Editor window will pop up where you can add and remove tab pages and you can also set these tab pages properties and events. I add four tab pages as you can see from Figure 6.

TabControl6.gif

Figure 6

As we have seen earlier in this tutorial, we can also add and remove tab pages to a TabControl using TabPage class.

Adding Child Controls to Tab Pages

Adding child controls to tab pages at design-time is pretty straight forward. You select a tab page and drag child controls from Toolbox onto a TabPage and set their properties. Accessing these child controls from code is no different than any other controls.

Adding child controls at run-time is little tricky. You create child controls and add them to the TabPage, not to the form.

The following snippet creates a Button control and adds it to a TabPage.

C# Code:

// Create a Button and add it to TabPage1

Button button1 = new Button();
button1.Name = "button1";
button1.Text = "Click Me";
button1.BackColor = Color.Blue;
button1.ForeColor = Color.White;
button1.Font = new Font("Verdana", 12);
button1.Width = 100;
button1.Height = 30;
button1.Location = new Point(50, 50);

// Add Button control to TabPage
tabPage1.Controls.Add(button1);

 

Display Images in Tab Control

To display images in in the header of tab pages at design-time, follow these steps.

  • Drag an ImageList control onto a Form
  • Add images to ImageList by clicking on its Images property
  • Set TabControl.ImageList property to ImageList1
  • Set TabPage.ImageIndex property to the index of image in ImageList. Remember, ImageList index starts at 0.

To set image in the tab pages header at run-time, first we need to create an ImageList and add images to it.

C# Code:

ImageList iconsList = new ImageList();
iconsList.TransparentColor = Color.Blue;
iconsList.ColorDepth = ColorDepth.Depth32Bit;
iconsList.ImageSize = new Size(25, 25);
iconsList.Images.Add(Image.FromFile(@"C:\Images\Garden.jpg"));
iconsList.Images.Add(Image.FromFile(@"C:\Images\Tree.jpg"));
iconsList.Images.Add(Image.FromFile(@"C:\Images\Waterfall.jpg"));

 

After that we set ImageList property of TabControl.

C# Code:

dynamicTabControl.ImageList = iconsList;

 

The last step is to set ImageIndex property of TabPages. 

C# Code:

tabPage1.ImageIndex = 0;


A TabControl with image icons looks like Figure 7.

 

TabControl7.gif

Figure 7

TabCount and RowCount

TabCount property gets the number of tab pages in a TabControl. RowCount property gets the number of rows that are currently being displayed in the control's tab strip.

C# Code:

MessageBox.Show(dynamicTabControl.TabCount.ToString());
MessageBox
.Show(dynamicTabControl.RowCount.ToString());

 

Selected Tab

SelectedTab and SelectedIndex properties get the selected tab and index of the selected tab in a TabControl.

C# Code:

TabPage selectedTab = dynamicTabControl.SelectedTab;
int
selectedIndex = dynamicTabControl.SelectedIndex;
dynamicTabControl.SelectedTab = selectedTab;

 

Summary

In this article, we discussed discuss how to create and use a TabControl control. First we discussed how to create a TabControl and add tab pages to TabControl at design-time and run-time. After that we saw, how to set its properties.


Login to add your contents and source code to this article
share this article :
post comment
 

hello Mahesh, I need your help on a project I´m doing now and its about tabcontrol. My project it for a doctor who needs to control patiens info in a simple way. I have created a tabcontrol with 4 pages inside the tabcontrol as you explained on your example here. Now, my question will be: how do I do to share or maintain the same info I have in one specific textbox in all 4 pages. That textbox info is the patient code which will be the same code and unique in all 4 pages. At this time I have to enter on ever page the same textbox data over and over and I need some code to stop doing this, so when I go to any tabcontrol page the patient code will be displayed already. Any ideas are very welcome and greetings for you

Posted by MARIO FERNANDEZ Feb 15, 2012

i 'm new in c sharp and i make an application consist of a tab control and every tabpage contain a grid view to display some data from a MS access file , i want to know the name of the data grid view when i select a specific tab .. can any one help me

Posted by Ahmed elbadry Jul 15, 2011

that is very nice article,it contain's many information

Posted by Suresh Naidu Jun 15, 2011

I agree! This site is a great source of ideas. I was looking through my VS2010 C# books to try and get an idea of a basic framework for a multi-tab application with a shared resource such as a serial port. Ports are tricky because of the callback nature of receive data. Sending from one tab can be easy, but then if you change tabs in the middle of a receive you have to remember who to focus the receive data to. An there could be separate parsing for each panel depending on the underlying business logic (as you suggest). Just a nice general framework for setting up the enviroment as such would be the basis for a nice article that expands on this one.

Posted by Steve Dame Apr 13, 2011

A form is part of the UI; for large applications forms should not be the entire application. Things such as serial port IO should be a separate class. I assume that Mahesh will have more advice but my guess is that one thing he would recommend is to look at the articles in this web site; there are hundreds of articles to help you. There is something called a three-tier application design (or something like that) that uses a Data Access Layer (DAL), Business Logic Layer (BLL) and the UI. The concept is relevant here; especially the concept of using a UI only for the UI portion.

Posted by Sam Hobbs Apr 13, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
Team Foundation Server Hosting
Become a Sponsor