You are on page 1of 3

.NET Events - What are object sender & EventArgs e?

Q. What do sender and eventArgs mean/refer to?


Q. How can I make use of them (for the scenario below)?

Scenario:

I'm trying to build a custom control with a delete function, and I want to
be able to delete the control that was clicked on a page that contains
many of the same custom control.

The sender is the control that the action is for (say OnClick, it's the
button).
The EventArgs are arguments that the implementor of this event may find
useful.
With OnClick it contains nothing good, but in some events, like say in a
GridView 'SelectedIndexChanged', it will contain the new index, or some
other useful data.

Well "you" are the class sending the event. So typically you subclass 'EventArgs'
with your own 'MyControlsEventEventArgs' and then set properties on that for
what you want to pass. So you only put things in here when you're writing the
control sending the event. If you want to put a string value in the button you
typically use the 'CommandArgument' field, which you can access by casting the
sender to 'Button' (as in my post).

sender refers to the object that invoked the event that fired the event handler.
This is useful if you have many objects using the same event handler.

EventArgs is something of a dummy base class. In and of itself it's more or less
useless, but if you derive from it, you can add whatever data you need to pass to
your event handlers.

When you implement your own events, use an EventHandler or


EventHandler<T> as their type. This guarantees that you'll have exactly these
two parameters for all your events (which is a good thing).
Manually cast the sender to the type of your custom control, and then use it to delete or
disable etc. Eg, something like this:

The 'sender' is just the object that was actioned (eg clicked).

The event args is subclassed for more complex controls, eg a treeview, so that you can
know more details about the event, eg exactly where they clicked.

1. 'sender' is called object which has some action perform on some control

2. 'event' its having some information about control which has some behavoiur and
identity perform by some user.when action will generate by occuring for event add it
keep within array is called event agrs

Source :stackoverflow
what is the meaning of ( object
sender, System.EventArgs e )

when they are together as parameters of function of event of a button, like :

public void rotation_Click(object sender, System.EventArgs e);


Under .NET framework, by convention, all events have this format.

The reasoning for the first argument is that a single event handler can be
attached to the same event like so.

[code]
btnTestA.Click += new EventHandler(btnTest_Click);
btnTestB.Click += new EventHandler(btnTest_Click);
[/code]

When btnTestA and btnTestB is clicked, the btnTest_Click event handler will be
fired. The "object sender" portion will be a reference to whatever one of the
buttons was clicked ...

[code]
private void btnTest_Click(object sender, EventArgs e)
{
Button test = sender as Button;

// etc ...
}
[/code]

The EventArgs portion is a way to pass extra information to the event handler. In
the case above, EventArgs is a base class and no real extra information is
passed. However, alot of events use a derivative of EventArgs, which contain
extra information.

The "sender" argument is the object that raised the event. The EventArgs
argument is extra information passed to the event.

So if u clicked a button, then the sender would be the button that u have clicked.

Plain and simple.

Now the fact that ALL events follow this format is a matter of convention. It is a
coding practice / pattern adopted by Mirosoft.

You might also like