Check if a specific tab page is selected (active)

后端 未结 6 1640
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 05:43

I am making an event to check if specific tab page in a tab control is active.

The point is, it will trigger an event if that tab page in a tab control is the curren

相关标签:
6条回答
  • 2020-12-14 05:52

    This can work as well.

    if (tabControl.SelectedTab.Text == "tabText" )
    {
        .. do stuff
    }
    
    0 讨论(0)
  • 2020-12-14 06:00

    Assuming you are looking out in Winform, there is a SelectedIndexChanged event for the tab

    Now in it you could check for your specific tab and proceed with the logic

    private void tab1_SelectedIndexChanged(object sender, EventArgs e)
    {
         if (tab1.SelectedTab == tab1.TabPages["tabname"])//your specific tabname
         {
             // your stuff
         }
    }
    
    0 讨论(0)
  • 2020-12-14 06:12

    I think that using the event tabPage1.Enter is more convenient.

    tabPage1.Enter += new System.EventHandler(tabPage1_Enter);
    
    private void tabPage1_Enter(object sender, EventArgs e)
    {
        MessageBox.Show("you entered tabPage1");
    }
    

    This is better than having nested if-else statement when you have different logic for different tabs. And more suitable in case new tabs may be added in the future.

    Note that this event fires if the form loads and tabPage1 is opened by default.

    0 讨论(0)
  • 2020-12-14 06:15

    For whatever reason the above would not work for me. This is what did:

    if (tabControl.SelectedTab.Name == "tabName" )
    {
         .. do stuff
    }
    

    where tabControl.SelectedTab.Name is the name attribute assigned to the page in the tabcontrol itself.

    0 讨论(0)
  • To check if a specific tab page is the currently selected page of a tab control is easy; just use the SelectedTab property of the tab control:

    if (tabControl1.SelectedTab == someTabPage)
    {
    // Do stuff here...
    }
    

    This is more useful if the code is executed based on some event other than the tab page being selected (in which case SelectedIndexChanged would be a better choice).

    For example I have an application that uses a timer to regularly poll stuff over TCP/IP connection, but to avoid unnecessary TCP/IP traffic I only poll things that update GUI controls in the currently selected tab page.

    0 讨论(0)
  • 2020-12-14 06:18

    in .Net 4 can use

    if (tabControl1.Controls[5] == tabControl1.SelectedTab)
                    MessageBox.Show("Tab 5 Is Selected");
    

    OR

    if ( tabpage5 == tabControl1.SelectedTab)
             MessageBox.Show("Tab 5 Is Selected");
    
    0 讨论(0)
提交回复
热议问题