简体   繁体   中英

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?

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.

您可以使用CommandArgument属性 ,然后将事件处理程序中的发送者强制转换为Button类型以获取CommandArgument值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM