TabControl is made from two parts:

  1. TabItem which represents the actual tab that contains image and text
  2. TabControlPanel which is connected to the tab through TabItem.AttachedControl property and which hosts your controls

The easiest way to add the tab and associated panels is by using the TabControl.CreateTab method. It will return the reference to the TabItem object and you can use then TabItem.AttachedControl property to access the tab panel. To the panel then you can add your controls for example like this TabItem.AttachedControl.Controls.Add(myControl). Here is sample code:

C#:

TabItem newTab = tabControl1.CreateTab("New Tab");
TabControlPanel panel = (TabControlPanel)newTab.AttachedControl;
TextBox textBox = new TextBox();
textBox.Location = new Point(10, 10);
panel.Controls.Add(textBox);

VB:

Dim newTab As TabItem = tabControl1.CreateTab("New Tab")
Dim panel As TabControlPanel = DirectCast(newTab.AttachedControl, TabControlPanel)
Dim textBox As New TextBox()
textBox.Location = New Point(10, 10)
panel.Controls.Add(textBox)

To select a tab item using code:

C#:

tabControl1.SelectedTabIndex = 1;

VB:

tabControl1.SelectedTabIndex = 1

To remove tabs remove TabItem from the TabControl.Tabs collection and remove the TabControl.Controls.Remove(tabItem.AttachedControl)

C#:

TabItem itemToRemove = tabControl1.Tabs[1];
tabControl1.Tabs.Remove(itemToRemove);
tabControl1.Controls.Remove(itemToRemove.AttachedControl);
tabControl1.RecalcLayout();

VB:

Dim itemToRemove As TabItem = tabControl1.Tabs(1)
tabControl1.Tabs.Remove(itemToRemove)
tabControl1.Controls.Remove(itemToRemove.AttachedControl)
tabControl1.RecalcLayout()

Great source of information is also InitializeComponent method inside of your form. If you drop the TabControl onto the form and set it up the way you want it to appear with controls and everything else, you can explore the InitializeComponent method on the form which will contain all the code to create what you see on the screen.

Related posts:

  1. How to change TabControl tab and panel colors
  2. How To Create NavigationPane Buttons Using Code
  3. How to Create Toolbars Using Code
  4. How to Create Menu Using Code
  5. What is InitializeComponent() method and how to use it to learn how to create controls from code