Friday, April 29, 2011

Passing a Param to System.EventHandler in 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?

From stackoverflow
  • 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.

    corymathews : Worked like a charm. Thanks.
  • you can use CommandArgument property and then cast a sender in your event handler to a Button type to get the CommandArgument value

0 comments:

Post a Comment