简体   繁体   English

使用代码切换到TabControl中的选项卡

[英]Switching to a tab in TabControl using code

I have a tabcontrol in my application that has several tabs in it. 我的应用程序中有一个tabcontrol,其中包含多个选项卡。

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. 使用TabControl.SelectedTab属性。 MSDN . MSDN

tabControl1.SelectedTab = anotherTab;

But you can also use the TabControl.SelectedIndex property. 但是,您也可以使用TabControl.SelectedIndex属性。 MSDN . MSDN

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

For this particular scenario you can use SelectedIndex property of the TabControl . 对于此特定方案,可以使用TabControl SelectedIndex属性。 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;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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