简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM