什么是C Sharp EventArgs e [英] What is C Sharp EventArgs e

查看:88
本文介绍了什么是C Sharp EventArgs e的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释一下 EventArgs e 是什么。感谢您的宝贵回答。

Please explain me what is EventArgs e. Thank you for your valuable answer.

protected void btnReportView_Click(object sender, EventArgs e)
   {
       Param();
       string strUrl = "wbFrmReportViewer.aspx?RepName=HolidayBillMIS&Date=" + dptdate.Text.Trim() + "&Param=" + strParam;
       Response.Redirect(strUrl);
   }

推荐答案

事件处理方法总是具有相同的签名(返回类型+参数)。

在WPF中,发件人是触发事件的用户控件,可能是通过鼠标点击。

但是在代码中你不必使用发送者变量。

如果它是一个文本框,那么它很有用。然后,您可以将sender对象强制转换为文本框,然后访问text属性。如果您的程序只有1个按钮或只有1个连接到特定回调的东西,那么您就知道谁是发送者,因此使用它有点多余。



EventArgs是一种基本类型,可能包含额外数据。

发件人可以附加自定义派生的EventArg类,其中包含有关回调的特定信息。图形用户控件可以例如发送X和Y坐标。 treelistview事件回调可以提供有关旧选定值和新选项的信息。

在这些情况下,您将签名从EventArgs更改为其他内容,或者您​​之后将其转换为方法正文中的顺序访问该信息。如果你使用选项卡功能,Visual Studio应该生成正确的EventArgs类



如果你只有一个开始按钮,你通常会忽略EventArg参数。你是什么想要做的就是调用其他一些代码。所以在某些情况下,两个参数都可以被忽略。
Event handling methods always has the same signature (return type + parameters).
In WPF, sender is the user control that triggers the event, maybe from a mouse click.
But in the code you don't have to use the sender variable.
It is useful if it is a textbox for example. Then you can cast the sender object into a textbox, and then access the text property. If your program only has 1 button or only 1 thing connected to a specific callback you kind of know who is the sender, so the use of it is a bit redundant.

EventArgs is a basetype, that may contain extra data.
A sender may attach a custom derived EventArg class, that contain specific info about the callback. A graphical usercontrol may for example send the X and Y coordinates. A treelistview event callback may supply info about the old selected value and the new.
In those cases, you change the signature from EventArgs to something else, or you cast it afterwards in in the method body in order to access that information. If you use the tab feature, Visual Studio should generate the correct EventArgs class

If you just have a button "Start", you usually ignore the EventArg parameter.What you want to do is call some other code. So in some cases both parameters may be ignored.


当一个事件发出信号时,有两个参数从信号器传递给处理程序:

sender 表示生成信号的类或控件实例,因此您可以为五个不同的按钮设置相同的处理程序,并确定它在处理程序中的哪个按钮: br />
When an event is signalled, there are two parameters which are passed through from the signaller to the handler:
sender which indicates the class or control instance which generated the signal so you can have the same handler for five different buttons for example, and determine which button it was in the handler:
Button b = sender as Button;
if (b != null)
    {
    Console.WriteLine(b.Text);
    }









e 其中包含有关该事件的其他信息,可用作添加处理程序所需信息的基础。



基本EventArgs没有什么特别有用的,并且通常通过信号传递为null,但是如果需要的话,它可以通过派生来扩展以添加信息。



假设你是例如你有一个类获取了股票价格的数据:您可以为CompanyA提供一个实例,为CompanyB提供一个实例,因此 sender 参数将告诉处理程序更新哪个公司,并且您可以扩展EventArgs以包括现货价格:因此,当您获得该事件时,您可以将正确的数据添加到正确的公司。即使价格变化非常快,每次发出事件信号时,都会保留当时的实际数据,供处理程序使用。



And

e which contains additional information about the event, and can be used as a basis for adding information that the handler needs.

The basic EventArgs has nothing particularly useful, and is often passed as null by signalling, but it can be extended by derivation to add information if it is needed.

Suppose for example you had a class which took a data feed of stock prices: You could have one instance for CompanyA and one for CompanyB, so the sender parameter would tell the handler which company to update, and you could extend the EventArgs to include the spot price: so when you got the event you could add the right data to the correct company. Even if the price changes very quickly, each time the event is signalled the actual data at that moment is preserved for the handler to work with.


这篇关于什么是C Sharp EventArgs e的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆