Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 645
JanOlMajti Avatar asked May 10 '12 06:05

JanOlMajti


2 Answers

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;
    }
}
like image 111
Nikhil Agrawal Avatar answered Oct 02 '22 19:10

Nikhil Agrawal


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.

like image 32
Muhammad Abbas Avatar answered Oct 02 '22 19:10

Muhammad Abbas