C# – Create an event for tab pages in C#

ctabcontroltabpagewinforms

I am developing an application in C# Windows Forms, and I would like to create an event handler/event handlers based on whether or not a particular tab page of a tab control is selected. So for example, if I have three tab pages:

tabPage1,
tabPage2,
tabPage3,

which belong to

tabControl1,

I need the code to either:

  1. Have three separate event handlers for each tab page
  2. Have one event handler and inside of the event handler there is code which can determine the tab page that is currently selected (e.g. a case statement of some sort)

I have looked at several examples thus far, but none seem to do what I need. How can I create this event/ these events?

Best Answer

May be something like this:

Make use of TabControl.Selected

private void tabControl1_Selected(Object sender, TabControlEventArgs e) 
{

   if(e.TabPage == tabPage1) 
     DoSomethingInRelationOfTab1();
   else if(e.TabPage == tabPage2)
     DoSomethingInRelationOfTab2();
   ....
   ....
}
Related Topic