How to determine which object raised an event

Posted on 2009-04-27
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 85

    Accepted Solution

    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;
        // do something with "tb"...

    Open in new window


    Author Comment

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

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    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…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now