C# “对象发送者”和“EventArgs e”参数有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14479143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:57:17  来源:igfitidea点击:

What is the use of "object sender" and "EventArgs e" parameters?

c#asp.net

提问by Qasim

In case of Page_Load, Initand other page events, what is the use of these (object sender, EventArgs e)parameters?

Page_Load,Init和其他页面事件的情况下,这些(object sender, EventArgs e)参数有什么用?

Examples would be more helpful.

例子会更有帮助。

采纳答案by Vinayak Pahalwan

EventArgs eis a parameter called e that contains the event data, see the EventArgs MSDN page for more information.

EventArgs e是一个名为 e 的参数,其中包含事件数据,有关详细信息,请参阅 EventArgs MSDN 页面。

Object Senderis a parameter called Sender that contains a reference to the control/object that raised the event.

Object Sender是一个名为 Sender 的参数,它包含对引发事件的控件/对象的引用。

Event Arg Class: http://msdn.microsoft.com/en-us/library/system.eventargs.aspx

事件参数类:http: //msdn.microsoft.com/en-us/library/system.eventargs.aspx

Example:

例子:

protected void btn_Click (object sender, EventArgs e){
   Button btn = sender as Button;
   btn.Text = "clicked!";
}

Edit:When Button is clicked, the btn_Click event handler will be fired. The "object sender" portion will be a reference to the button which was clicked

编辑:单击 Button 时,将触发 btn_Click 事件处理程序。“对象发送者”部分将是对被点击按钮的引用

回答by Jamiec

Those two parameters (or variants of) are sent, by convention, with all events.

按照惯例,这两个参数(或其变体)与所有事件一起发送。

  • sender: The object which has raised the event
  • ean instance of EventArgsincluding, in many cases, an object which inherits from EventArgs. Contains additional information about the event, and sometimes provides ability for code handling the event to alter the event somehow.
  • sender: 引发事件的对象
  • eEventArgs在许多情况下,包括一个从 继承的对象的实例EventArgs。包含有关事件的附加信息,有时还提供处理事件的代码以某种方式改变事件的能力。

In the case of the events you mentioned, neither parameter is particularly useful. The is only ever one page raising the events, and the EventArgsare Emptyas there is no further information about the event.

对于您提到的事件,这两个参数都不是特别有用。将始终只提高了活动页面,并且EventArgsEmpty因为有关于该事件没有进一步的消息。

Looking at the 2 parameters separately, here are some examples where they areuseful.

在两个参数分别看,这里有一些例子,他们有用的。

sender

sender

Say you have multiple buttons on a form. These buttons could contain a Tagdescribing what clicking them should do. You could handle all the Clickevents with the same handler, and depending on the senderdo something different

假设您在一个表单上有多个按钮。这些按钮可以包含Tag描述点击它们应该做什么。您可以Click使用相同的处理程序处理所有事件,并根据不同的情况sender做一些不同的事情

private void HandleButtonClick(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    if(btn.Tag == "Hello")
      MessageBox.Show("Hello")
    else if(btn.Tag == "Goodbye")
       Application.Exit();
    // etc.
}

Disclaimer : That's a contrived example; don't do that!

免责声明:这是一个人为的例子;不要那样做!

e

e

Some events are cancelable. They send CancelEventArgsinstead of EventArgs. This object adds a simple boolean property Cancelon the event args. Code handling this event can cancel the event:

有些活动是可以取消的。他们发送CancelEventArgs而不是EventArgs. 该对象Cancel在事件参数上添加了一个简单的布尔属性。处理此事件的代码可以取消该事件:

private void HandleCancellableEvent(object sender, CancelEventArgs e)
{
    if(/* some condition*/)
    {
       // Cancel this event
       e.Cancel = true;
    }
}