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.
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.
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 detail 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.
- // 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;
- Controls.Add(dynamicTabControl);
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.
- // 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);
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 the Properties menu item. The Properties window looks like Figure 4.
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.
- dynamicTabControl.Name = "DynamicTabControl";
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.
- dynamicTabControl.Dock = DockStyle.Left;
Font property represents the font of text of a TabControl control. If you click on the Font property in the Properties window, you will see Font name, size, and other font options. The following code snippet sets Font property at run-time.
- dynamicTabControl.Font = new Font("Georgia", 16);
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.
- dynamicTabControl.BackColor = Color.White;
- dynamicTabControl.ForeColor = Color.Black;
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 has Normal, Buttons, or FlatButtons values.
- dynamicTabControl.Alignment = TabAlignment.Left;
- dynamicTabControl.Appearance = TabAppearance.FlatButtons;
TabPages property, a type of TabPageColleciton object is the gateway to access and add tab pages to a TabControl. Like any other collection, 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.
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.
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 a 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.
- // 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);
To display images 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 the image in ImageList. Remember, ImageList index starts at 0.
To set an image in the tab pages header at run-time, first, we need to create an ImageList and add images to it.
- 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"));
- dynamicTabControl.ImageList = iconsList;
- tabPage1.ImageIndex = 0;
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.
- MessageBox.Show(dynamicTabControl.TabCount.ToString());
- MessageBox.Show(dynamicTabControl.RowCount.ToString());
SelectedTab and SelectedIndex properties get the selected tab and index of the selected tab in a TabControl.
- 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.

DG HofPosted Oct 10, 2019, 3:05 AM
How do you color the background of the TabControl it self not the tabs
felix connelyPosted Apr 27, 2015, 10:27 AM
Im new on that sector.. i m just junior about that.. so i ve a question.. i ve tabcontrols and there is a gridview.. i ve got my database' rows itinto that gridview. Its working.. but i tried almost today about deleting.. but i cant solve it! Could you please give a sample code how i can delete the selected rows in c# side to delete from database. Thank you for helping.
Jose phPosted Feb 16, 2015, 3:25 AM
Sorry, I forgot to tell that my Project is a web application.
Jose phPosted Feb 11, 2015, 5:11 AM
Hi I like very much your article. I have a Project in visual studio 2003. This Project use TabScrip Control. Nowadays, I am changing my Project to visual studio 2012 but I have a problema my TabScrip control give an error in execution time error. My problema is from Page1 go to Page2 (Where is my control). First the page execute server code (Page_load) after the page2 is going to doing in the client side and before of the creation of TabScrip is showed an error. 0x800a138f it can not be obtained property "__TabStrip1_State__". In my opini?n This happens because between internet explorer 8 to internet explorer 11 has changed the method to read DOM document. I can tell too when I execute my Project step by step my execution is stopped when is going to create TabStrip control. "<TD vAlign="top" width="100%"><script language="javascript">document.Form1.__TabStrip1_State__.value=0;<". I'm a little tired because I don't found a solution because I don't know like changing my code because this line is showed in execution time. Thanks in advance. I hope someone can help me to fix my problema.
Hisoka YamaPosted Jul 23, 2014, 3:02 AM
I have tab control and datagridview. When tabcontrol page change, grid must be refill data. But data isn't refilled. How to solve this issue.
Revathy choPosted Oct 21, 2013, 4:17 AM
This article useful to me.Thank U Sooo Much Mahesh. :)
MARIO FERNANDEZPosted Feb 15, 2012, 5:21 PM
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
Ahmed elbadryPosted Jul 15, 2011, 11:40 AM
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
Suresh NaiduPosted Jun 15, 2011, 3:22 AM
that is very nice article,it contain's many information
Steve DamePosted Apr 13, 2011, 1:17 PM
Thanks for this nice article Mahesh. Could you expand a bit on how to properly set up good class structure for each tab? I find myself creating a separate class file for each tab for complex code behavior on the tabs, but then having to define a partial to share information for the whole form. I also have a system where I am using a serial port control on one tab and utilizing the serial port I/O on each of the other tabs. It gets really messy and everything has to be global to the "form". I'm thinking there must be a better way, but haven't had time to refactor and explore how to compartmentalize better.
Sam HobbsPosted Apr 4, 2011, 12:01 AM
Thank you, Mahesh; I will enjoy using this information when I have the opportunity. I wrote an article like this many years ago about use of the CTabCtrl in C++ MFC. In the Windows API the Tab control (CTabCtrl in C++ MFC) has (at least had) a bug that causes a problem when a ListView control (CListCtrl class in C++ MFC) is used in a tab page. I am not sure which of the two actually has (or had) the bug and I don't know it it has been fixed but it was a known problem for so long that I assume it was never fixed. I also assume that the .Net classes don't have a problem, for whatever reason. If anyone knows of a problem when a .Net ListView is used in a .Net TabControl then I hope they will share what they know.
Santosh Kumar KotnalaPosted Apr 3, 2011, 11:26 PM
that contain useful information........