object sender, EventArgs e

Posted on 2006-05-03
Last Modified: 2008-02-26
Anyone know what object sender, EventArgs e mean in an event handler?

Does it matter that they might not be used?
Whats the point of them if they're not used?
Question by:paulwhelan
    LVL 52

    Accepted Solution

    sender is the object that raised the event. EventArgs can contain information related to the event.

    A lot of the time they are not used, but they do have their uses. Also note that different event types can have different Args types (i.e. it may not be EventArgs it could be another type).

    For example, you could have an EventHandler that handles the same event for multiple controls. Using sender you can determine which particulat control raised the event. So, say you have two buttons and both use the same eventhandler to handle their click event; you could use something like the following to determine which button was clicked:

        Button btn = (Button)sender;

        if (btn.Name = "button1")
            // it was button 1
            // it was button 2
    LVL 4

    Expert Comment

    "object sender, EventArgs e"
    First, it was create in Visual Studio.NET just be auto-generated, so you can find it wherever there is an EventHandler.
    Second, You need the two Parameters as for a EventHandler which was pre-ruled by MS.NET.

    The question is why we have 2 but not 3 or else. Because we see that it's enough for us to process the event.
    - object sender helps you to specify the control which raise the event.
    - EventArgs contains data for the event when raised.

    If you don't give an EventHandler two proper parameters as required, you may not compile your application.
    LVL 6

    Expert Comment

    here's an example of how to use sender

    Put 2 buttons on a form.

    Go into code view and create the following method by hand (ie: it's the same kind of code that would be generated when you double click a button and go to code view, but instead of doing that, just type it)

    private void GenericButton_Click(object sender, System.EventArgs e)
        MessageBox.Show((sender as Button).Text);

    Now go back into design view and highlight the first button. Go to your properties window (you can hit F4 for a shortcut to your properties window when you have a control selected) and click on the lightning bolt so that you are now looking at all of the events. Select the click event, but don't double click on it or else you will generate a event handler, instead just select it and hit the drop-down button to bring up a list of compatible event handlers for that button. Select GenericButton_Click from the list. Do the same thing for your other button. Notice that you can drag as many buttons on the form as you like, as long as you go to the events in the properties window and select "GenericButton_Click" from the drop-down, all of those buttons will tie into the event.

    Run the program and notice that all of the buttons are tied will show a messagebox which contains the text of that button. That is because the button is passed to the event handler (GenericButton_Click) as the sender. In the code for that you are saying

    MessageBox.Show((sender as Button).Text);

    you have to say (sender as Button) because we want the event handler to know that a button is being passed through (casting the sender object) Because sender is an object it can be anything... a textbox a listbox, whatever. But since we are using buttons we have to cast it to a button. We could also have done:

    MessageBox.Show(((Button)sender).Text);  but it's a little bit harder to read if you are just learning this stuff (IMO).

    It's a little hard to grasp a concept like this when you're brand new because the way it looks, The button raises a click event and the form has an event handler for that event that is raised... they are actually decoupled from eachother even though it doesn't quite seem that way when  you are first learning about this.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 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

    17 Experts available now in Live!

    Get 1:1 Help Now