C# - modify button.click event to pass data?
I am programatically creating buttons and each butto开发者_如何学Cn has a tag binary array. While creating I am binding an event Button.Click
but I do not know how to add parameter since the event handler is already prepared. I would need to pass the tag of the button to method that is called by that event.
You don't need to pass the tag of the button - the button is provided as the sender, so you can get at the tag directly:
private void HandleButtonClick(object sender, EventArgs e)
{
Button button = (Button) sender;
object tag = button.Tag;
...
}
Another alternative is to wire up the event manually with an anonymous method or lambda expression, which lets you call another method with a more suitable signature:
button.Click += (s, e) => SaveDocument(someLocalVariable);
In that example, someLocalVariable
is local to the method wiring up the events - it could be an instance variable of course, but then you don't really need to pass it as you'd have access anyway.
Get your tag like this
byte[] myData = ((Button)sender).Tag
精彩评论