Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

Multiple handlers for an event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dom - 07 May 2007 19:42 GMT
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.
Bruce Wood - 07 May 2007 19:47 GMT
> In CSharp, can you have multiple handlers for a single event?  For
> example, for a textbox TextChanged event, can I have two separate
[quoted text clipped - 3 lines]
>
> 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.
Dom - 07 May 2007 19:52 GMT
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?

> > In CSharp, can you have multiple handlers for a single event?  For
> > example, for a textbox TextChanged event, can I have two separate
[quoted text clipped - 12 lines]
> does not guarantee this, and so it's not behaviour you should ever
> depend upon.
Jon Skeet [C# MVP] - 07 May 2007 20:23 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dom - 07 May 2007 20:29 GMT
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.

> > 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
[quoted text clipped - 6 lines]
> Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Bruce Wood - 07 May 2007 20:54 GMT
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.
>
[quoted text clipped - 12 lines]
> > Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> > If replying to the group, please do not mail me too
Dom - 07 May 2007 20:59 GMT
Yes, I thought of that too, and I'm starting to think you're right.

> 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
[quoted text clipped - 45 lines]
> > > Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeetBlog:http://www.msmvps.com/jon.skeet
> > > If replying to the group, please do not mail me too
Jon Skeet [C# MVP] - 07 May 2007 21:09 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.