Add tabs to a TabControl at runtime in C#

[owner-drawn TabControl]

The post Make an improved owner-drawn TabControl in C# shows how to build an owner-drawn TabControl that lets the user remove tabs at runtime. This example lets you add tabs, too.

The program includes a Label that displays an X. When you click the Label, the following code adds the new tab,

// Add a tab to the end of the TabControl.
private void lblAddTab_Click(object sender, EventArgs e)
    Console.WriteLine("Adding a new tab");

    TabPage page = new TabPage("New");
    page.BackColor = Color.LightBlue;

    Label label = new Label();
    label.Location = new Point(0, 0);
    label.Text = "Tab number " + (tabMenu.TabPages.Count - 1);
    label.AutoSize = true;

This code displays a message in the Console window. It then creates a TabPage object and adds it to the TabControl‘s TabPages collection.

Next the code creates a Label, sets its Location and Text properties, and adds it to the TabPage‘s Controls collection. (In a real application, this is where you would create whatever controls you want the tab to contain.)

The TabControl still behaves strangely sometimes, particularly if it’s too small to display all of the tabs at once. See the previous post for more information.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in controls and tagged , , , , , , , , , , . Bookmark the permalink.

2 Responses to Add tabs to a TabControl at runtime in C#

  1. Kumar says:

    How to integrate the WPF forms in Windows Tab controls using c#
    Please provide me the reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.