Home > other >  select tabpages in tabControl C#
select tabpages in tabControl C#

Time:07-12

private void tabPage2_Click(object sender, EventArgs e)
        {
        }

hello i have one tabconrol in my form this name is "tabControl1" & i have 4 tabpage in tabControl1 i want when i click on tabpage1 run some code when i click tabpage2 run some code too & ...

how can i ?

i think if condition is not true or i dont know i must wite code in which event

i use tabPage1_Click event

tnx for help

CodePudding user response:

I believe you're looking for the SelectedIndexChanged event:

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show(tabControl1.SelectedIndex.ToString());
}

This will display 0, 1, 2, 3 etc. depending on which tab page is selected. First one is 0, second is 1 and so on.

CodePudding user response:

You can try this code:

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string nameOfSlecetedTab = tabControl1.SelectedTab.Name;
        int indexOfSelectedTab = tabControl1.SelectedIndex;

        switch(indexOfSelectedTab)
        {
            case 0:
                //code for tabPage0
                break;
            case 1:
                //code for tabPage1
                break;
            default:
                break;
        }
    }

CodePudding user response:

You can register on tabControls clicked event

    private void tabControl1_Click(object sender, EventArgs e)
    {
        MouseEventArgs args = e as MouseEventArgs;

        if (args.Button == MouseButtons.Left)
        {
            if (tabControl1.GetTabRect(0).Contains(args.Location))
            {
                //tabPage1 header clicked
            }
            else if (tabControl1.GetTabRect(1).Contains(args.Location))
            {
                //tabPage2 header clicked
            } 
        }
        
    }
  • Related