TabPage Click Events

Rob picture Rob · Mar 13, 2012 · Viewed 47.9k times · Source

I'm trying to automatically trigger events based on the tab page that is clicked on the tab control.

In design mode of my form, when I click on the tabs the properties box says Tabs System.Windows.Forms.TabControl whichever tab is selected. However, I have to click on the actual page, not the tab for the property to change to the name of the pages e.g. TaskListPage System.Windows.Forms.TabPage.

My tabcontrol is called Tabs and I was trying to test it out using the code below which is supposed to display a message based on the tab option.

 private void Tabs_SelectedIndexChanged(object sender, EventArgs e)

        {
            if (Tabs.SelectedTab == TaskListPage)
            {
                MessageBox.Show("TASK LIST PAGE");
            }
            else if (Tabs.SelectedTab == SchedulePage)
            {
                MessageBox.Show("SCHEDULE PAGE");
            }
        }

When I test the code above, nothing is happening.

Any help in getting the events working when a specific tab is clicked would be greatly appreciated!

Thankyou

Answer

LarsTech picture LarsTech · Mar 13, 2012

It sounds like you don't have it wired up:

public Form1() {
  InitializeComponent();    
  Tabs.SelectedIndexChanged += new EventHandler(Tabs_SelectedIndexChanged);
}

There are other events that can give you this information as well: Selected and Selecting.

void Tabs_Selected(object sender, TabControlEventArgs e) {
  if (e.TabPage == TaskListPage) {
    // etc
  }
}