Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabPage Click Events

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

like image 823
Rob Avatar asked Mar 13 '12 12:03

Rob


1 Answers

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
  }
}
like image 132
LarsTech Avatar answered Oct 12 '22 03:10

LarsTech