How to determine which object raised an event

Posted on 2009-04-27
Medium Priority
Last Modified: 2012-05-06
Hi Guys,
I have several  text boxes that trap the "MouseEnter" event but they all subscribe the same event handler. (See code below).  My question is, when the MouseHandler method is called, how do I tell which textbox raised the event?
public Form1()
    textBox1.MouseEnter += new EventHandler(MouseHandler);
    textBox2.MouseEnter += new EventHandler(MouseHandler);
    textBox3.MouseEnter += new EventHandler(MouseHandler);
    textBox4.MouseEnter += new EventHandler(MouseHandler);
void MouseHandler(object sender, EventArgs e)
    //How do I determine which textbox raised the event

Open in new window

Question by:GavinS
LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 24246745
The "sender" parameter tells you which TextBox fired the event.  You can cast it back to TextBox to make decisions if need be:

void MouseHandler(object sender, EventArgs e)
    TextBox tb = (TextBox)sender;
    // ...now do something with "tb"...

Open in new window


Author Comment

ID: 24246823
Arhh, Now I can use "tb.Name"   -Thanks

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question