[Mono-winforms-list] Is it possible to overloadtheSystem.EventHandler?

Jonathan Gilbert 2a5gjx302 at sneakemail.com
Mon Feb 5 00:05:42 EST 2007


At 09:04 PM 2/3/2007 +0000, Paul wrote:
>Hi,
>
[snip]
>Basically, I want to pass another parameter (in this case, an instance
>of a delegate class) to the event raised. I've tried doing quite a few
>things, but nothing seems to happen.

Two other things, in addition to the other comments so far:

1. The "sender" parameter to the delegate will give you the control on
which the event happened. This probably doesn't solve your problem, but it
does, for instance, allow the same event handler to be used with multiple
controls. You'd of course only do this if the action to be taken were
identical -- for instance, if you had a handful of textboxes on a form and
you wanted them to autoformat phone numbers, you could wire up all of their
TextChanged and Validate events to the same set of handlers, and implement
the logic just once. Of course, this would be more properly done by
creating a subclass of TextBox.. :-)

2. C# 2.0's "delegate() {}" syntax can be used to automatically persist
variables from one method into another acting as a callback. For instance:

  txtFoo.TextChanged +=
    delegate(object sender, EventArgs e)
    {
      HandleTextChangedEvent(sender, e, arg2);
    };

Then all you need to do is define a HandleTextChangedEvent that takes the 3
parameters. This "trampoline" is represented by the compiler by
automatically generating a class to contain the delegate and extend the
lexical scoping (if that terminology is right -- picked it up from a friend
who is hot on Perl 6 and such) of "arg2", pretty much what Kornel proposed
with the wrapper object. I'm pretty sure with this syntax it keeps whatever
value arg2 had at the point of the "delegate() {}" bit; changing the value
after that line won't change the value passed to HandleTextChangedEvent()
when the event is raised.

Perhaps this can help :-)

Jonathan Gilbert


More information about the Mono-winforms-list mailing list