tags:

views:

110

answers:

2

Hi, I am programatically creating buttons and each button has a tag binary array. While creation I am binding an event Button.Click but I do not know how to add parameter since the eventhandler is already prepared. I would need to pass the tag of the button to method that is called by that event.Thank you!

+5  A: 

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.

Jon Skeet
As Jon Skeet points out, there's a very direct way for that to happen.However, I'd question why you're storing a binary array as a buttons Tag. Is there perhaps a better way to do it?
Jason D
A: 

Get your tag like this

byte[] myData = ((Button)sender).Tag
ArsenMkrt