How to find which TabControl tab is on

What is the easiest way to find a tab. I want to show some data when I click on tabpage2 or some other tab. I did it like this, but this is not a good solution:

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 to display data:

  if (findTabPage == 1) { some code here } if (findTabPage == 2) { some code here } 

Is there any other solution like this?

+7
source share
3 answers

Using

 tabControl1.SelectedIndex; 

This will give you the selected tab index, which will start at 0 and go up to 1 less than the total number of your tabs

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; } } 
+10
source

Just use tabControl1.SelectedIndex :

 if (tabControl1.SelectedIndex == 0) { some code here } if (tabControl1.SelectedIndex == 1) { some code here } 
+3
source

This is a much better approach.

 private int CurrentTabPage { get; set; } private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { CurrentTabPage = tabControl1.SelectedIndex; } 

This way, every time tabindex changes, our required CurrentTabPage will be automatically updated.

+2
source

All Articles