Add parameter to an event trap
The following illustrates how to add an additional parameter to an event trap.
Create a method that takes the standard parameters of an Event Handler, plus the extra parameter. In this case, the parameter is an integer.
void MyEventTrapWithParameter(object sender, EventArgs e, int MyExtraParam) { // do some stuff here }
Our method needs to be specified during event registration. However, the standard Event Handler takes only 2 parameters. Use a lambda expression as an adapter. This can be done in 2 ways.
void Test() { // Declare an instance of an integer variable that we want to pass to the event handler. int lSchID = 5; // the SchedulerFired event handler expects only 2 parameters. Use a lambda expression as an adapter. this.sch.SchedulerFired += new EventHandler((sender2, ev) => this.MyEventTrapWithParameter(sender2, ev, lSchID)); // shorthand for the above. Works the same. this.sch.SchedulerFired += (sender2, ev) => this.MyEventTrapWithParameter(sender2, ev, lSchID); }