简体   繁体   English

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

[英]How do I programmatically select a tab on a .NET CF TabControl?

With the .NET Framework 2.0/3.5 TabControl, I can programmatically select a tab using the SelectedTab property as shown in the code below: 使用.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;
}

The .NET Compact Framework TabControl doesn't have a SelectedTab property like its .NET Framework counterpart. .NET Compact Framework TabControl没有像.NET Framework对应项那样具有SelectedTab属性。 So, how do I select a tab programmatically? 那么,如何以编程方式选择标签?

TabControl.SelectedIndex

I programmed this code. 我编写了这段代码。 When click on tabPage1, then the program will close: 单击tabPage1时,程序将关闭:

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

in .Net 4 can use .Net 4中可以使用

if (tabControl1.Controls[5] == tabControl1.SelectedTab)
                MessageBox.Show("Tab 5 Is Selected");

OR 要么

if ( tabpage5 == tabControl1.SelectedTab)
         MessageBox.Show("Tab 5 Is Selected");

I found that when the TabControl is selected, it does not display correctly. 我发现选择TabControl时,它无法正确显示。 It seems that after selecting a TabControl it is useful to Refresh it. 似乎在选择TabControl之后,刷新它很有用。 So, where the TabControl is called TabForm and has multiple Tabs, this might be: 因此,在TabControl称为TabForm且具有多个Tabs的情况下,可能是:

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

WPF code, try this: WPF代码,请尝试以下操作:

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

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

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