Tab Control in C#

Introduction

TabControl manages and collects a collection of tabs related to the user that can contain controls and elements. It is usually used to divide a Windows form and unlimited tabs can be created in TabControl. A tab page is an actual control that hosts other child controls.

Creating a RadioButton

We can create a TabControl using a Forms designer or runtime using the TabControl class.

Forms designer

Let's create a TabControl in forms designer, at first you create a windows form, then goto the toolbox = > drag the TabControl and drop on the form.

creating a tabcontrol

Runtime

Let's create a RadioButton in Runtime,then you use the RadioButton class.

private void InitializeComponent()
{
    this.tabControl1 = new System.Windows.Forms.TabControl();
    this.tabPage1 = new System.Windows.Forms.TabPage();
    this.tabPage2 = new System.Windows.Forms.TabPage();
    this.tabControl1.SuspendLayout();
    this.SuspendLayout();
    //
    // tabControl1
    //
    this.tabControl1.Controls.Add(this.tabPage1);
    this.tabControl1.Controls.Add(this.tabPage2);
    this.tabControl1.Location = new System.Drawing.Point(46, 29);
    this.tabControl1.Name = "tabControl1";
    this.tabControl1.SelectedIndex = 0;
    this.tabControl1.Size = new System.Drawing.Size(433, 220);
    this.tabControl1.TabIndex = 0;
    // 
    // tabPage1
    //
    this.tabPage1.Location = new System.Drawing.Point(4, 22);
    this.tabPage1.Name = "tabPage1";
    this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
    this.tabPage1.Size = new System.Drawing.Size(425, 194);
    this.tabPage1.TabIndex = 0;
    this.tabPage1.Text = "tabPage1";
    this.tabPage1.UseVisualStyleBackColor = true;
    // 
    // tabPage2
    // 
    this.tabPage2.Location = new System.Drawing.Point(4, 22);
    this.tabPage2.Name = "tabPage2";
    this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
    this.tabPage2.Size = new System.Drawing.Size(192, 74);
    this.tabPage2.TabIndex = 1;
    this.tabPage2.Text = "tabPage2";
    this.tabPage2.UseVisualStyleBackColor = true;
    // 
    // Form1
    //
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(592, 317);
    this.Controls.Add(this.tabControl1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.tabControl1.ResumeLayout(false);
    this.ResumeLayout(false);
}

Properties of TabControl

We will learn about all the properties of the tabcontrol and use them.

  • Alignment

    The Alignment properties tabs are used to determine which side of the tabcontrol to show.There are basically four sides here they are top,right,bottom,left. It can be used both run time and compile time.

    Form design of Alignment :
    alignment of tabcontrol
    Run-time design of Alignment :

    Tabs are appear in top side.

    tabControl1.Alignment = TabAlignment.Top;
    
    top alignment of tabcontrol

    Tabs are appear in bottom side.

    tabControl1.Alignment = TabAlignment.Bottom;
    
    bottom alignment of tabcontrol

    Tabs are appear in right side.

    tabControl1.Alignment = TabAlignment.Right;
    
    right alignment of tabcontrol

    Tabs are appear in left side.

    tabControl1.Alignment = TabAlignment.Left;
    
    left alignment of tabcontrol
  • Appearance

    The Appearance property indicates whether the tabs are painted as buttons or regular tabs. It can be used both run time and compile time.

    Form design of appearance :
    appearance of tabcontrol
    Run-time design of appearance :

    Tabs are appear in normal.

    tabControl1.Appearance = TabAppearance.Normal;
    
    normal appearance of tabcontrol

    Tabs are appear in button.

    tabControl1.Appearance = TabAppearance.Buttons;
    
    button appearance of tabcontrol

    Tabs are appear in flat buttons.

    tabControl1.Appearance = TabAppearance.FlatButtons;
    
    flatbuttons appearance of tabcontrol
  • HotTrack

    HotTrack indiacting whether the tabs visually change when the mouse passes over them. It can be used both run time and compile time.

    hottrack of tabcontrol
    tabControl1.HotTrack = true;
    
  • Font

    We will use the font property to change the text style of tabcontrol tabs set text. Inside the font property is the font family,font size,font style and so on. It can be used both run time and compile time.

    Font in  tabcontrol
    tabControl1.Font = new Font("cursive",12F);
    
  • Location

    Location property to change the location of tabControl. It can be used both run time and compile time.

    Location in  tabControl
    tabControl1.Location = new Point(150, 100);
    
  • Size

    Size property to change the size of tabControl. It can be used both run time and compile time.

    Size in  tabcontrol
    tabControl1.Size = new Size(150, 150);
    
  • TabPages

    TabPages properties add or remove tabs in the tabControl. It can be used both run time and compile time.

    Form design of Tabpages :

    At first you right click on the tabControl = > then click properties = > then select TabPages and click them.

    tabpages in  tabcontrol

    In this dialog box you can add tabs by simple steps. First you click the Add button = > set the text button. You remove any tabs then just select the tab and click the remove button.

    tabpages dialog in  tabcontrol
    Run-time design of TabPages
    //Adding tabPages
    tabControl1.TabPages.Add("File");
    //Remove tabPages
    tabControl1.TabPages.Remove(tabPage3);
    
  • Adding Control in Tabpages

    When you want to add any control into TabPage. Then you select the tabpage that are you want to use. then drag any control into toolbox and drop the control in tabpage.

    Form design of TabControl :
    add control in  tabcontrol
    Label lbl = new Label();
    lbl.Text="This is tab Page 2";
    tabPage2.Controls.Add(lbl);
    
    Run-time design of TabControl :
    add control runtime in  tabcontrol


Write a comment


Name Email Message