简体   繁体   中英

Working with “object sender, EventArgs e” inside an Event Handler

When I manually cast the object sender and Eventargs e to a class like below what is it I am exactly doing? I know that it allows me to access all the arguments that have been passed and also to manipulate the object sender (as below):

private void Button1_Click(object sender, EventArgs e)
{
    / /casting the arguments
    MouseEventArgs eventargs = e as MouseEventArgs;
    Button button1 = sender as Button;
    // displays which mouse button I used
    MessageBox.Show(eventargs.Button.ToString());
    // displays the name of the button I clicked
    MessageBox.Show(button1.Name.ToString());
    // changes the text of the button
    button1.Text = "Ive changed";
}

I feel like I don't understand how this works, only that it works.

Also, it seems quite easy to write an event handler that serves several objects of the same type, but not one that can handle different types of event or different types of object ie:

private void Generic_Event_Handler(object sender, EventArgs e)
{
    // displays what object and event triggered the handler
    MessageBox.Show(sender.ToString());
    MessageBox.Show(e.ToString());
}        

Is this ever used? Is there a decent explanation of eventhandlers out there?

The following signature

private or protected void EventHandlersName (object sender, EventArgs e)

is the signature that they have all the event handlers in .NET . The parameter called sender is associated with the object on which the event called e was raised.

Why the type of sender is object ?

Because all types in .NET has as their base type the type System.Object . So this way it doesn't make any difference if we have a click event on button on a win form, or in a WPF applciation or in an ASP.NET web form button.

Why we should manually cast the object sender?

We should do so, in order we have access to the properties and the methods of the specific type we have in each case.

For instance, all controls may not have a property called Name . Furthermore the base type called System.Object doesn't have. So if you don't cast the object sender to a Button class, then you can't read it's property called Name .

The signature of an event handler in .Net is (or at least should be):

(object sender, XXArgs e) where XXArgs is a class that inherits from EventArgs .

sender is, well, the sender of the event. In your example, if you click on a button, the button will fire the event using its own instance reference ( this ) for the sender parameter (so sender is a reference to your button). This is useful because you don't need to store references to the sender of the event ; it's available right here in the event handler.

For the XXArgs part, it contains information about the event. You shouldn't cast it actually, but write your handler with the right signature. Basically, for a mouse click on a button , the right signature of the event handler would be:

private void Control_MouseClick(Object sender, MouseEventArgs e)
{
}

sender is a reference to type object . The actual object that it refers to could be anything; (in the specific case of an event handler, if everything is working as intended it should refer to the control that generated the event). When you say:

Button button1 = sender as Button;

you are trying to use a Button reference to refer to the same underlying object. If the underlying object is compatible (ie: is a Button ), then the button1 reference will enable you to utilize the Button members of the underlying object. Otherwise, with the sender reference, you would only have been able to utilize the object members of the underlying object.

Note that it could still be something more "concrete" or specific than a Button , and then you would need a different cast to get at the more derived members.

Also you can cast page in case of protected void Page_Load(object sender, EventArgs e)

var senderInfo = (Page)sender;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM