How to find which tab page (TabControl) is on

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:

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?

Answer

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

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;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *