sa_leinad sa_leinad - 4 months ago 25
C# Question

Switching to a tab in TabControl using code

I have a tabcontrol in my application that has several tabs in it.

I want to automatically switch to another tab when the "Next" button is pressed.

I cannot figure out how to change which tab is visible programmatically.

private void Next_Click(object sender, EventArgs e)
{
// Change to the next tab
tabControl1.???;
}

Answer

Use the TabControl.SelectedTab property. MSDN.

tabControl1.SelectedTab = anotherTab;

But you can also use the TabControl.SelectedIndex property. MSDN.

try
{
    tabControl1.SelectedIndex += 1;
}
catch
{
    //This prevents the ArgumentOutOfRangeException.
}