object sender, EventArgs e

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?
Who is Participating?
Carl TawnSystems and Integration DeveloperCommented:
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
"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.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.