如何以编程方式选择.NET CF TabControl上的选项卡?

时间:2009-05-08 18:02:02

标签: c# .net compact-framework tabcontrol

使用.NET Framework 2.0 / 3.5 TabControl,我可以使用SelectedTab属性以编程方式选择一个选项卡,如下面的代码所示:

//toggles between tabPage1 and tabPage2
private void button1_Click(object sender, EventArgs e)
{
    if (tabControl1.SelectedTab == tabPage1)
        tabControl1.SelectedTab = tabPage2;
    else
        tabControl1.SelectedTab = tabPage1;
}

.NET 压缩框架TabControl没有像其.NET Framework对应的SelectedTab属性。那么,如何以编程方式选择选项卡?

4 个答案:

答案 0 :(得分:21)

TabControl.SelectedIndex

答案 1 :(得分:1)

我编写了这段代码。单击tabPage1时,程序将关闭:

private void tabControl1_MouseClick(object sender, MouseEventArgs e)
    {
        if (tabControl1.SelectedTab == tabPage1)
        {
            MessageBox.Show("Logout!");
            Application.Exit();
        }
    }

答案 2 :(得分:0)

我发现当选择TabControl时,它无法正确显示。看来在选择TabControl之后刷新它很有用。因此,TabControl被称为TabForm并具有多个Tabs,这可能是:

 Me.TabForm.SelectedIndex = 0
 Me.TabPg0.Refresh            'Where TabPg0 is the name of the Tab at Index 0

答案 3 :(得分:-2)

WPF代码,试试这个:

if (tabControl1.SelectedValue == tabPage1)
    tabControl1.SelectedValue = tabPage2;
else
    tabControl1.SelectedValue = tabPage1;