Can someone please explain to me in the most layman terms how to use EventArgs?

隐身守侯 提交于 2019-12-11 00:46:45

问题


I know they have something to do with delegates. I've tried but I still don't comprehend how to use those either. I know a little about event handlers but what I really want to know is how I can use plain old eventargs that are part of most methods. example below

void Page_Load(object sender, EventArgs e)
{
myText.Value = "Hello World!";
}

Thanks for your time & consideration, I am just trying to be the best coder I can be.

Mike


回答1:


EventArgs classes are used as data carriers when raising events. They typically contain information that is related to the event being raised.

Many events use the EventArgs class, which contains no particular information. This class also serves as the base class for all other EventArgs classes. One example of a more specific EventArgs class is the TreeNodeEventArgs class, that is used by a number of events, and that contains information about which TreeNode that the event is related to.

In some cases, EventArgs classes can be designed so that they allow event handlers to communicate data back to the source that raised the event, an example of that is the CancelEventArgs class.




回答2:


The base EventArgs class is an emtpy class that you can't use directly.

However, there are many derived EventArgs classes that provide data about an event.

For example, the KeyPress event gives a KeyPressEventArgs which tells you which key was pressed.

If you get an event handler where the e parameter is an inherited EventArgs, you can use the properties on the inherited EventArgs object to find out more about the event.




回答3:


Plain old EventArgs are not really that useful, however types that derive from EventArgs are much more useful since you can define other members to carry strongly-typed data.

A good example of this is the GridViewEditEventArgs that are passed by a GridView inherit from EventArgs but extend it to provide NewEditIndex and Cancel properties.



来源:https://stackoverflow.com/questions/1778952/can-someone-please-explain-to-me-in-the-most-layman-terms-how-to-use-eventargs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!