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?
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.
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.
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.
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.
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.
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