C# 如何找到哪个标签页(TabControl)在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10528842/
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 to find which tab page (TabControl) is on
提问by JanOlMajti
What is the easyest way to find which tab is on. I want to show some data when i click on tabpage2 or some other tabpage. I did it like this but is not good solution:
找到哪个选项卡的最简单方法是什么。当我点击 tabpage2 或其他一些 tabpage 时,我想显示一些数据。我是这样做的,但不是很好的解决方案:
private int findTabPage { get; set; }
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabPage1)
findTabPage = 1;
if (tabControl1.SelectedTab == tabPage2)
findTabPage = 2;
}
and for displaying data:
并用于显示数据:
if (findTabPage == 1)
{ some code here }
if (findTabPage == 2)
{ some code here }
Is there any other solution for example like this?
有没有其他解决方案,例如这样的?
采纳答案by Nikhil Agrawal
Use
用
tabControl1.SelectedIndex;
This will give you selected tab index which will start from 0 and go till 1 less then the total count of your tabs
这将为您提供选定的标签索引,该索引将从 0 开始,直到标签总数减少 1
Use it like this
像这样使用它
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
switch(tabControl1.SelectedIndex)
{
case 0:
{ some code here }
break;
case 1:
{ some code here }
break;
}
}
回答by Daniel Hilgarth
Simply use tabControl1.SelectedIndex:
只需使用tabControl1.SelectedIndex:
if (tabControl1.SelectedIndex == 0)
{ some code here }
if (tabControl1.SelectedIndex == 1)
{ some code here }
回答by Muhammad Abbas
This is a much better approach.
这是一个更好的方法。
private int CurrentTabPage { get; set; }
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
CurrentTabPage = tabControl1.SelectedIndex;
}
In this way every time the tabindex is changed, our required CurrentTabPage would automatically updated.
这样每次更改 tabindex 时,我们所需的 CurrentTabPage 都会自动更新。

