TabControl.SelectedIndex Property

Definition

Gets or sets the index of the currently selected tab page.

C#
[System.ComponentModel.Browsable(false)]
public int SelectedIndex { get; set; }

Property Value

The zero-based index of the currently selected tab page. The default is -1, which is also the value if no tab page is selected.

Attributes

Exceptions

The value is less than -1.

Examples

The following code example creates a TabControl with two TabPage objects. The SelectedIndex property sets tabPage2 as the currently selected tab page using its index value.

C#
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
    private TabControl tabControl1;
    private TabPage tabPage1;
    private TabPage tabPage2;

    private void MyTabs()
    {
        this.tabControl1 = new TabControl();
        this.tabPage1 = new TabPage();
        this.tabPage2 = new TabPage();

        this.tabControl1.Controls.AddRange(new Control[] {
            this.tabPage1,
            this.tabPage2});
        this.tabControl1.Padding = new Point(15, 10);
        this.tabControl1.Location = new Point(35, 25);
        this.tabControl1.Size = new Size(220, 220);

        // Selects tabPage2 using SelectedIndex.
        this.tabControl1.SelectedIndex = 1;
    
        this.tabPage1.Text = "myTabPage1";
        this.tabPage1.TabIndex = 0;

        this.tabPage2.Text = "myTabPage2";
        this.tabPage2.TabIndex = 1;

        this.Size = new Size(300, 300);
        this.Controls.AddRange(new Control[] {
            this.tabControl1});
    }
    
    public Form1()
    {
        MyTabs();
    }

    static void Main() 
    {
        Application.Run(new Form1());
    }
}

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also