C# 如何以编程方式选择 .NET CF TabControl 上的选项卡?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/841064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I programmatically select a tab on a .NET CF TabControl?
提问by raven
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 CompactFramework TabControl doesn't have a SelectedTab property like its .NET Framework counterpart. So, how do I select a tab programmatically?
.NET CompactFramework TabControl 不像它的 .NET Framework 对应物那样有 SelectedTab 属性。那么,如何以编程方式选择选项卡?
采纳答案by Cheeso
TabControl.SelectedIndex
TabControl.SelectedIndex
回答by Mark Kadlec
WPF code, try this:
WPF 代码,试试这个:
if (tabControl1.SelectedValue == tabPage1)
tabControl1.SelectedValue = tabPage2;
else
tabControl1.SelectedValue = tabPage1;
回答by rafa?
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();
}
}
回答by ali
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");
回答by Neil Dunlop
I found that when the TabControl is selected, it does not display correctly. It seems that after selecting a TabControl it is useful to Refresh it. So, where the TabControl is called TabForm and has multiple Tabs, this might be:
我发现选择TabControl时,显示不正确。似乎在选择 TabControl 后刷新它很有用。因此,在 TabControl 称为 TabForm 并具有多个选项卡的情况下,这可能是:
Me.TabForm.SelectedIndex = 0
Me.TabPg0.Refresh 'Where TabPg0 is the name of the Tab at Index 0