Export (0) Print
Expand All
1 out of 8 rated this helpful - Rate this topic

TabControl Class

Manages a related set of tab pages.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class TabControl : Control
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class TabControl extends Control
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class TabControl extends Control
Not applicable.

A TabControl contains tab pages, which are represented by TabPage objects that you add through the TabPages property. The order of tab pages in this collection reflects the order the tabs appear in the control.

The user can change the current TabPage by clicking one of the tabs in the control. You can also programmatically change the current TabPage by using one of the following TabControl properties:

In Microsoft .NET Framework version 2.0, you can also use one of the following methods:

In .NET Framework 2.0, you can respond when the current tab changes by handling one of the following events:

The tabs in a TabControl are part of the TabControl, but not parts of the individual TabPage controls. Members of the TabPage class, such as the ForeColor property, affect only the client rectangle of the tab page, but not the tabs. Additionally, the Hide method of the TabPage will not hide the tab. To hide the tab, you must remove the TabPage control from the TabControl.TabPages collection.

In .NET Framework 2.0, the tab is considered part of the tab page for determining when the Enter and Leave events of the TabPage occur. In earlier versions of the .NET Framework, the Enter and Leave events of the TabPage do not occur when focus enters or leaves a tab, but only when focus enters or leaves the client rectangle of the tab page.

The following events are not raised for the TabControl class unless there is at least one TabPage in the TabControl.TabPages collection: Control.Click, Control.DoubleClick, Control.MouseDown, Control.MouseUp, Control.MouseHover, Control.MouseEnter, Control.MouseLeave and Control.MouseMove. If there is at least one TabPage in the collection, and the user interacts with the tab control's header (where the TabPage names appear), the TabControl raises the appropriate event. However, if the user interaction is within the ClientRectangle of the tab page, the TabPage raises the appropriate event.

Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown.

When visual styles are enabled, and the Alignment property is set to a value other than Top, the tab contents might not render correctly. To work around this issue, you can paint the tab contents yourself using owner drawing. For more information, see How to: Display Side-Aligned Tabs with TabControl.

When the Alignment property is set to a value other than Top and the Appearance property is set to a value other than Normal, the tab page contents might not render correctly.

The following code example uses the Visual Studio .NET Windows Forms Designer to create a TabControl with three tab pages. Each tab page contains several controls.

public class Form1 : System.Windows.Forms.Form
{
    // Required designer variable.
    private System.ComponentModel.Container components;

    // Declare variables.
    private System.Windows.Forms.RadioButton tab3RadioButton2;
    private System.Windows.Forms.RadioButton tab3RadioButton1;
    private System.Windows.Forms.CheckBox tab2CheckBox3;
    private System.Windows.Forms.CheckBox tab2CheckBox2;
    private System.Windows.Forms.CheckBox tab2CheckBox1;
    private System.Windows.Forms.Label tab1Label1;
    private System.Windows.Forms.Button tab1Button1;
    private System.Windows.Forms.TabPage tabPage3;
    private System.Windows.Forms.TabPage tabPage2;
    private System.Windows.Forms.TabPage tabPage1;
    private System.Windows.Forms.TabControl tabControl1;
 
    public Form1()
    {
        // This call is required for Windows Form Designer support.
        InitializeComponent();
    }
 
 
    // This method is required for Designer support.
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.tabPage1 = new System.Windows.Forms.TabPage();
        this.tab2CheckBox3 = new System.Windows.Forms.CheckBox();
        this.tab3RadioButton2 = new System.Windows.Forms.RadioButton();
        this.tabControl1 = new System.Windows.Forms.TabControl();
        this.tab2CheckBox2 = new System.Windows.Forms.CheckBox();
        this.tab2CheckBox1 = new System.Windows.Forms.CheckBox();
        this.tab3RadioButton1 = new System.Windows.Forms.RadioButton();
        this.tab1Label1 = new System.Windows.Forms.Label();
        this.tabPage3 = new System.Windows.Forms.TabPage();
        this.tabPage2 = new System.Windows.Forms.TabPage();
        this.tab1Button1 = new System.Windows.Forms.Button();

        tabPage1.Text = "tabPage1";
        tabPage1.Size = new System.Drawing.Size(256, 214);
        tabPage1.TabIndex = 0;
        tab2CheckBox3.Location = new System.Drawing.Point(32, 136);
        tab2CheckBox3.Text = "checkBox3";
        tab2CheckBox3.Size = new System.Drawing.Size(176, 32);
        tab2CheckBox3.TabIndex = 2;
        tab2CheckBox3.Visible = true;
        tab3RadioButton2.Location = new System.Drawing.Point(40, 72);
        tab3RadioButton2.Text = "radioButton2";
        tab3RadioButton2.Size = new System.Drawing.Size(152, 24);
        tab3RadioButton2.TabIndex = 1;
        tab3RadioButton2.Visible = true;
        tabControl1.Location = new System.Drawing.Point(16, 16);
        tabControl1.Size = new System.Drawing.Size(264, 240);
        tabControl1.SelectedIndex = 0;
        tabControl1.TabIndex = 0;
        tab2CheckBox2.Location = new System.Drawing.Point(32, 80);
        tab2CheckBox2.Text = "checkBox2";
        tab2CheckBox2.Size = new System.Drawing.Size(176, 32);
        tab2CheckBox2.TabIndex = 1;
        tab2CheckBox2.Visible = true;
        tab2CheckBox1.Location = new System.Drawing.Point(32, 24);
        tab2CheckBox1.Text = "checkBox1";
        tab2CheckBox1.Size = new System.Drawing.Size(176, 32);
        tab2CheckBox1.TabIndex = 0;
        tab3RadioButton1.Location = new System.Drawing.Point(40, 32);
        tab3RadioButton1.Text = "radioButton1";
        tab3RadioButton1.Size = new System.Drawing.Size(152, 24);
        tab3RadioButton1.TabIndex = 0;
        tab1Label1.Location = new System.Drawing.Point(16, 24);
        tab1Label1.Text = "label1";
        tab1Label1.Size = new System.Drawing.Size(224, 96);
        tab1Label1.TabIndex = 1;
        tabPage3.Text = "tabPage3";
        tabPage3.Size = new System.Drawing.Size(256, 214);
        tabPage3.TabIndex = 2;
        tabPage2.Text = "tabPage2";
        tabPage2.Size = new System.Drawing.Size(256, 214);
        tabPage2.TabIndex = 1;
        tab1Button1.Location = new System.Drawing.Point(88, 144);
        tab1Button1.Size = new System.Drawing.Size(80, 40);
        tab1Button1.TabIndex = 0;
        tab1Button1.Text = "button1";
        tab1Button1.Click += new 
           System.EventHandler(this.tab1Button1_Click);
        this.Text = "Form1";

        // Adds controls to the second tab page.
        tabPage2.Controls.Add(this.tab2CheckBox3);
        tabPage2.Controls.Add(this.tab2CheckBox2);
        tabPage2.Controls.Add(this.tab2CheckBox1);
        // Adds controls to the third tab page.
        tabPage3.Controls.Add(this.tab3RadioButton2);
        tabPage3.Controls.Add(this.tab3RadioButton1);
        // Adds controls to the first tab page.
        tabPage1.Controls.Add(this.tab1Label1);
        tabPage1.Controls.Add(this.tab1Button1);
        // Adds the TabControl to the form.
        this.Controls.Add(this.tabControl1);
        // Adds the tab pages to the TabControl.
        tabControl1.Controls.Add(this.tabPage1);
        tabControl1.Controls.Add(this.tabPage2);
        tabControl1.Controls.Add(this.tabPage3);
    }
 
    private void tab1Button1_Click (object sender, System.EventArgs e)
    {
        // Inserts the code that should run when the button is clicked.
    }
 
    public static void Main(string[] args) 
    {
        Application.Run(new Form1());
    }
}
      

public class Form1 extends System.Windows.Forms.Form
{
    // Required designer variable.
    private System.ComponentModel.Container components;

    // Declare variables.
    private System.Windows.Forms.RadioButton tab3RadioButton2;
    private System.Windows.Forms.RadioButton tab3RadioButton1;
    private System.Windows.Forms.CheckBox tab2CheckBox3;
    private System.Windows.Forms.CheckBox tab2CheckBox2;
    private System.Windows.Forms.CheckBox tab2CheckBox1;
    private System.Windows.Forms.Label tab1Label1;
    private System.Windows.Forms.Button tab1Button1;
    private System.Windows.Forms.TabPage tabPage3;
    private System.Windows.Forms.TabPage tabPage2;
    private System.Windows.Forms.TabPage tabPage1;
    private System.Windows.Forms.TabControl tabControl1;

    public Form1()
    {
        // This call is required for Windows Form Designer support.
        InitializeComponent();
    } //Form1

    // This method is required for Designer support.
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.tabPage1 = new System.Windows.Forms.TabPage();
        this.tab2CheckBox3 = new System.Windows.Forms.CheckBox();
        this.tab3RadioButton2 = new System.Windows.Forms.RadioButton();
        this.tabControl1 = new System.Windows.Forms.TabControl();
        this.tab2CheckBox2 = new System.Windows.Forms.CheckBox();
        this.tab2CheckBox1 = new System.Windows.Forms.CheckBox();
        this.tab3RadioButton1 = new System.Windows.Forms.RadioButton();
        this.tab1Label1 = new System.Windows.Forms.Label();
        this.tabPage3 = new System.Windows.Forms.TabPage();
        this.tabPage2 = new System.Windows.Forms.TabPage();
        this.tab1Button1 = new System.Windows.Forms.Button();
        tabPage1.set_Text("tabPage1");
        tabPage1.set_Size(new System.Drawing.Size(256, 214));
        tabPage1.set_TabIndex(0);
        tab2CheckBox3.set_Location(new System.Drawing.Point(32, 136));
        tab2CheckBox3.set_Text("checkBox3");
        tab2CheckBox3.set_Size(new System.Drawing.Size(176, 32));
        tab2CheckBox3.set_TabIndex(2);
        tab2CheckBox3.set_Visible(true);
        tab3RadioButton2.set_Location(new System.Drawing.Point(40, 72));
        tab3RadioButton2.set_Text("radioButton2");
        tab3RadioButton2.set_Size(new System.Drawing.Size(152, 24));
        tab3RadioButton2.set_TabIndex(1);
        tab3RadioButton2.set_Visible(true);
        tabControl1.set_Location(new System.Drawing.Point(16, 16));
        tabControl1.set_Size(new System.Drawing.Size(264, 240));
        tabControl1.set_SelectedIndex(0);
        tabControl1.set_TabIndex(0);
        tab2CheckBox2.set_Location(new System.Drawing.Point(32, 80));
        tab2CheckBox2.set_Text("checkBox2");
        tab2CheckBox2.set_Size(new System.Drawing.Size(176, 32));
        tab2CheckBox2.set_TabIndex(1);
        tab2CheckBox2.set_Visible(true);
        tab2CheckBox1.set_Location(new System.Drawing.Point(32, 24));
        tab2CheckBox1.set_Text("checkBox1");
        tab2CheckBox1.set_Size(new System.Drawing.Size(176, 32));
        tab2CheckBox1.set_TabIndex(0);
        tab3RadioButton1.set_Location(new System.Drawing.Point(40, 32));
        tab3RadioButton1.set_Text("radioButton1");
        tab3RadioButton1.set_Size(new System.Drawing.Size(152, 24));
        tab3RadioButton1.set_TabIndex(0);
        tab1Label1.set_Location(new System.Drawing.Point(16, 24));
        tab1Label1.set_Text("label1");
        tab1Label1.set_Size(new System.Drawing.Size(224, 96));
        tab1Label1.set_TabIndex(1);
        tabPage3.set_Text("tabPage3");
        tabPage3.set_Size(new System.Drawing.Size(256, 214));
        tabPage3.set_TabIndex(2);
        tabPage2.set_Text("tabPage2");
        tabPage2.set_Size(new System.Drawing.Size(256, 214));
        tabPage2.set_TabIndex(1);
        tab1Button1.set_Location(new System.Drawing.Point(88, 144));
        tab1Button1.set_Size(new System.Drawing.Size(80, 40));
        tab1Button1.set_TabIndex(0);
        tab1Button1.set_Text("button1");
        tab1Button1.add_Click(new System.EventHandler(this.tab1Button1_Click));
        this.set_Text("Form1");

        // Adds controls to the second tab page.
        tabPage2.get_Controls().Add(this.tab2CheckBox3);
        tabPage2.get_Controls().Add(this.tab2CheckBox2);
        tabPage2.get_Controls().Add(this.tab2CheckBox1);

        // Adds controls to the third tab page.
        tabPage3.get_Controls().Add(this.tab3RadioButton2);
        tabPage3.get_Controls().Add(this.tab3RadioButton1);

        // Adds controls to the first tab page.
        tabPage1.get_Controls().Add(this.tab1Label1);
        tabPage1.get_Controls().Add(this.tab1Button1);

        // Adds the TabControl to the form.
        this.get_Controls().Add(this.tabControl1);

        // Adds the tab pages to the TabControl.
        tabControl1.get_Controls().Add(this.tabPage1);
        tabControl1.get_Controls().Add(this.tabPage2);
        tabControl1.get_Controls().Add(this.tabPage3);
    } //InitializeComponent

    private void tab1Button1_Click(Object sender, System.EventArgs e)
    {
        // Inserts the code that should run when the button is clicked.
    } //tab1Button1_Click
    
    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main
} //Form1

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
        System.Windows.Forms.TabControl
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.