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
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With