Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event args assigning

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 ??

like image 867
Miroo Avatar asked Dec 15 '25 09:12

Miroo


1 Answers

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.

like image 108
Jon Skeet Avatar answered Dec 16 '25 21:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!