.NET 事件 - 什么是对象发送者和 EventArgs e?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1303145/
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
.NET Events - What are object sender & EventArgs e?
提问by stringo0
What do sender and eventArgs mean/refer to? How can I make use of them (for the scenario below)?
sender 和 eventArgs 是什么意思/指的是什么?我如何使用它们(对于下面的场景)?
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.
我正在尝试使用删除功能构建自定义控件,并且我希望能够删除在包含许多相同自定义控件的页面上单击的控件。
回答by Noon Silk
The sender is the control that the action is for (say OnClick, it's the button).
发件人是操作所针对的控件(比如 OnClick,它是按钮)。
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.
EventArgs 是此事件的实现者可能会发现有用的参数。使用 OnClick 它不包含任何好处,但在某些事件中,例如在 GridView 'SelectedIndexChanged' 中,它将包含新索引或一些其他有用的数据。
What Chris is saying is you can do this:
克里斯所说的是你可以这样做:
protected void someButton_Click (object sender, EventArgs ea)
{
Button someButton = sender as Button;
if(someButton != null)
{
someButton.Text = "I was clicked!";
}
}
回答by Matthew Scharley
senderrefers 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.
sender指调用触发事件处理程序的事件的对象。如果您有许多对象使用相同的事件处理程序,这将非常有用。
EventArgsis 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.
EventArgs是一个虚拟的基类。就其本身而言,它或多或少是无用的,但是如果您从中派生,则可以添加任何需要传递给事件处理程序的数据。
When you implement your own events, use an EventHandleror EventHandler<T>as their type. This guarantees that you'll have exactly these two parameters for all your events (which is a good thing).
当您实现自己的事件时,请使用EventHandlerorEventHandler<T>作为它们的类型。这保证了您将拥有所有事件的这两个参数(这是一件好事)。
回答by Chris
Manually cast the sender to the type of your custom control, and then use it to delete or disable etc. Eg, something like this:
手动将发件人强制转换为自定义控件的类型,然后使用它来删除或禁用等。例如,如下所示:
private void myCustomControl_Click(object sender, EventArgs e)
{
((MyCustomControl)sender).DoWhatever();
}
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.
事件 args 被子类化以用于更复杂的控件,例如树视图,以便您可以了解有关事件的更多详细信息,例如他们单击的确切位置。
回答by rajeev kumar singh
'sender' is called object which has some action perform on some control
'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
'sender' 被称为对象,它对某些控件执行某些操作
'事件' 它有一些关于控制的信息,这些信息有一些用户执行的行为和身份。当动作将通过发生事件添加时生成,它保留在数组中称为事件 agrs
回答by John Saunders
FYI, senderand eare not specific to ASP.NET or to C#. See Events (C# Programming Guide)and Events in Visual Basic.
仅供参考,sender而e不是特定于ASP.NET或C#。请参阅事件(C# 编程指南)和Visual Basic 中的事件。

