views:

27

answers:

1

i have this event handler

Temp.MouseLeftButtonDown += new MouseButtonEventHandler(Temp_MouseLeftButtonDown);

but i wanna send some parameter to access in the Temp_MouseLeftButtonDown function. how can i assign it ??

+3  A: 

You can't do it directly, because the event handler can only expect a compatible signature with MouseButtonEventHandler.

If you're using C# 3, the easiest approach would be to use a lambda expression - something like:

Temp.MouseLeftButtonDown +=
   (sender, args) => Temp_MouseLeftButtonDown(sender, args, "extra argument");

Does that help? Of course, if you don't need both the sender and event args, you don't have to supply them.

In C# 2 you could use an anonymous method in the same way.

Jon Skeet
+1 great to know this, was wondering myself....
VoodooChild
Could someone says few good reason for wanting to have extra args in the hadler? please and thank you!!!
VoodooChild
@VoodooChild: You may want to use most of the same logic in different places, but with some variations.
Jon Skeet
@Jon: Thanx it worked like charm (Y)@VoodooChild: my case is creating buttons in runtime and each button has a different value i wanna handle that's why i wanted to pass this value with each method for each button.Just a comment i was getting an error when tried to pass the value using my loop i value but it worked fine when i put the value in a variable then passed this variable example: double value = Positions[i].AdPosition;Temp.MouseLeftButtonDown += (sender, args) => TempBreak_MouseLeftButtonDown(value);
Miroo