sa_leinad
sa_leinad

Reputation: 349

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.???;
    }

Upvotes: 1

Views: 126

Answers (2)

sujith karivelil
sujith karivelil

Reputation: 29036

For this particular scenario you can use SelectedIndex property of the TabControl. This gives you an integer representing the index of the currently selected tab. Likewise you can set a tab as selected by setting an integer value to this property.

private void btnNext_Click(object sender, EventArgs e)
{
   int currentTabIndex = tabControl1.SelectedIndex;
   currentTabIndex++;
   if (currentTabIndex < tabControl1.TabCount)
   {
      tabControl1.SelectedIndex = currentTabIndex;
   }
   else
   {
     btnNext.Enabled=false;
   }
}

Upvotes: 1

user6438653
user6438653

Reputation:

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.
}

Upvotes: 4

Related Questions