Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a Param to System.EventHandler in c#

Tags:

c#

Since google has failed me for the past 5-10 minutes I have a quick question. I wish to pass a param value into a function that I call from a button.click Event Handler. The event is currently added using

MyButton.Click = new System.EventHandler(MyButton_click);

But I want the function to recieve:

private void MyButton_click(int ID)
{
...
}

How can I change my EventHandler declaration so that this can be accomplished?

like image 801
corymathews Avatar asked Apr 15 '09 06:04

corymathews


People also ask

How do you pass a parameter to an event handler?

If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.

How do you add a parameter to an event?

To add custom parameters to automatically collected events, do one of the following: Disable and then manually recreate an event with additional parameters. Send a custom event specifically to capture additional parameters. Add custom parameters to all events.

What is the difference between an event and an EventHandler?

In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.

Is an EventHandler a delegate?

The EventHandler delegate is a predefined delegate that specifically represents an event handler method for an event that does not generate data. If your event does generate data, you must use the generic EventHandler<TEventArgs> delegate class.


1 Answers

Button.Click is defined as it is, and there is no way to change it. However,

myButton.Click += delegate { MyButton_click(1); }

will do the job.

like image 151
Anton Gogolev Avatar answered Sep 28 '22 03:09

Anton Gogolev