Multiple handlers for an event

D

Dom

In CSharp, can you have multiple handlers for a single event? For
example, for a textbox TextChanged event, can I have two separate
handlers called up? I have 4 text boxes, and the TextChanged events
all go to the same handler, but I want one of them to go to a separate
handler also.

This is done in Java, IIRC.
 
B

Bruce Wood

In CSharp, can you have multiple handlers for a single event? For
example, for a textbox TextChanged event, can I have two separate
handlers called up? I have 4 text boxes, and the TextChanged events
all go to the same handler, but I want one of them to go to a separate
handler also.

This is done in Java, IIRC.

Sure... just subscribe to the event a second time, and specify the
other handler. That's why C# uses the "+=" notation: to indicate that
you're adding a handler, not replacing one.

By the way, you have no control over the _order_ in which the handlers
are invoked. They are invoked in the order you add them, but the spec
does not guarantee this, and so it's not behaviour you should ever
depend upon.
 
D

Dom

Should I assume that this can't be done in the form's design window,
using the property box, but I have to go into the Form.Designer.cs
code, and actually add it by hand?
 
J

Jon Skeet [C# MVP]

Dom said:
Should I assume that this can't be done in the form's design window,
using the property box, but I have to go into the Form.Designer.cs
code, and actually add it by hand?

There's no need to do it in the designer's code - do it in your own
code after the designer initialization has executed.
 
D

Dom

Got it.

Out of curiosity, why didn't MS allow for this in the form's design
view, in the property box? That's where it's done in Java's IDE,
Netbeans.
 
B

Bruce Wood

Well, I can't speak for Microsoft, but I imagine it's because it's a
very rare case, and allowing for a single form to subscribe to an
event multiple times would result in 99 mistakes for every intended
usage of the feature.

I've built a lot of Windows forms. I often have multiple event
handlers for an event, but I've never had multiple handlers within a
form subscribing to the same control event.

In fact, if I understand your scenario, I would be more inclined to
code it as each event handler calling an internal routine to do the
real work, so if there are four controls raising events, and three of
them do the same thing while clicking the fourth control (or whatever)
does the same as the other three plus a bit more, I would code it like
this:

private void Control1_2_3_Click(object sender, System.EventArgs e)
{
DoSomething();
}

private void Control4_Click(object sender, System.EventArgs e)
{
... some additional code here ...
DoSomething();
}

which allows me to set the event handlers in the Designer GUI, _and_
gives me full control over the order in which things happen within the
fourth event handler.

Got it.

Out of curiosity, why didn't MS allow for this in the form's design
view, in the property box? That's where it's done in Java's IDE,
Netbeans.

There's no need to do it in the designer's code - do it in your own
code after the designer initialization has executed.
 
J

Jon Skeet [C# MVP]

Dom said:
Out of curiosity, why didn't MS allow for this in the form's design
view, in the property box? That's where it's done in Java's IDE,
Netbeans.

Small point off the topic of events, but to save confusion - there are
several Java IDEs. Netbeans is only one of them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top