Search code examples
c#event-handlingmouseeventeventargs

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


Solution

  • 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.