如何在事件处理程序中传递两个以上的参数 [英] How to pass more than two parameters in the event handler

查看:130
本文介绍了如何在事件处理程序中传递两个以上的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以建议我传递除两个参数以外的更多参数

事件如下吗?


活动:

Onbutton_click(对象发送者,EventArgs e)"


事件处理程序:

button.Click + = new EventHandler(Onbutton_click);


我想传递与该事件相关的更多信息。 &安培;想在活动中使用那个

的信息。

Can anyone suggest me to pass more parameters other than two parameter for
events like the following?

Event:
Onbutton_click(object sender, EventArgs e)"

Event handler:
button.Click += new EventHandler(Onbutton_click);

I want to pass more information related that event. & want to use that
information in the event.

推荐答案

Swappy< Sw **** @ discussion.microsoft .comwrote:
Swappy <Sw****@discussions.microsoft.comwrote:

任何人都可以建议我传递除两个参数以外的更多参数

事件,如下所示?


事件:

Onbutton_click(对象发送者,EventArgs e)"


事件处理程序:

button.Click + = new EventHandler(Onbutton_click);


我想传递与该事件相关的更多信息。 &安培;想在活动中使用那个

的信息。
Can anyone suggest me to pass more parameters other than two parameter for
events like the following?

Event:
Onbutton_click(object sender, EventArgs e)"

Event handler:
button.Click += new EventHandler(Onbutton_click);

I want to pass more information related that event. & want to use that
information in the event.



传递更多信息的正常方法是从EventHandler派生更丰富的类型




-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客: http://www.msmvps.com/jon_skeet

C#深度: http://csharpindepth.com

The normal way of passing more information is to make a richer type
derived from EventHandler.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com


周三,2008年7月16日22:47:01 -0700,Swappy

< Sw **** @ discussion.microsoft.comwrote:
On Wed, 16 Jul 2008 22:47:01 -0700, Swappy
<Sw****@discussions.microsoft.comwrote:

任何人都可以建议我传递更多参数而不是两个参数



事件如下?


事件:

Onbutton_click(对象发送者,EventArgs e)"


事件处理程序:

button.Click + = new EventHandler(Onbutton_click);
Can anyone suggest me to pass more parameters other than two parameter
for
events like the following?

Event:
Onbutton_click(object sender, EventArgs e)"

Event handler:
button.Click += new EventHandler(Onbutton_click);



次要尼特:我认为你的标签已被撤销。 :)事件是

" button.Click"和事件处理程序方法本身。

Minor nit: I think you have your labels reversed. :) The "Event" is
"button.Click" and the "Event handler" is the method itself.


我想传递更多与该事件相关的信息。 &安培;想在活动中使用那个

的信息。
I want to pass more information related that event. & want to use that
information in the event.



什么样的信息?它来自哪里?


通常的方法是使用带有事件处理程序的匿名方法

具有修改后的签名。例如:


void Onbutton_click(对象发送者,EventArgs e,int i){...}


button.Click + = delegate (对象发送者,EventArgs e)

{Onbutton_click(sender,e,172); };


当然,你不必传入172,或者甚至将第三个参数

变为int。 :)


但这是否有效取决于你试图传递的信息..

如果上面的例子没有导致为您提供即时解决方案,

可能希望提供有关您实际尝试的更多详细信息。


Pete

What kind of information? Where would it come from?

The usual approach is to use an anonymous method with an event handler
that has your modified signature. For example:

void Onbutton_click(object sender, EventArgs e, int i) { ... }

button.Click += delegate(object sender, EventArgs e)
{ Onbutton_click(sender, e, 172); };

Of course, you don''t have to pass in 172, or even make the third parameter
an int. :)

But whether that works depends on what information you''re trying to pass..
If the above example doesn''t lead to an immediate solution for you, you
might want to provide more details as to what you''re actually trying to do.

Pete


嘿,

Jon Skeet的建议是关于从事件args派生的内容和

让你自己代表该事件。这是.NET做的标准方式

并且它一定有什么问题。

虽然有些人认为它是

不适合解析对发送者和EventArgs派生类的引用

到一组只需要知道boolean或int的事件处理程序。他们认为,有很多推广的EventArgs引入了不必要的代码膨胀,这是浪费时间。

我个人尚未决定虽然我喜欢

对事件有标准''look'的想法,但我也同意解析事件所需数据的简单性。唯一的缺点是,如果你确定将来要添加第二条数据,你需要更改

事件,因此需要更改所有订阅者。 EventArgs确实绕过了你的b / b
。关于这个论点和一般事件的进一步信息,最近有一个关于它们的dotnetrocks插曲:
http://www.dotnetrocks.com/default.aspx?showNum=355


我会留给你自己提出解决方案。只是想到我

会让你意识到辩论的双方。

-

Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com

Swappy写道:
Hey,
Jon Skeet is right with his suggestion about deriving from event args and
making you own delegate for the event. That is the standard way .NET does it
and there is necessarily anything wrong with it.
There is a bit of debate about it though as some people suggest that it is
not ideal to parse a reference to the sender and an EventArgs derived class
to a set of event handlers that only need to know a boolean or and int. They
believe that having lots of derivations of EventArgs introduces unnecessary
code bloat and is a waste of time.
I am personally undecided on the matter as although I like the idea of
having a standard ''look'' for events, I also agree with the simplicity of just
parsing the needed data for events. The only down side of this is if you do
decide in the future to add a second piece of data, you need to change the
event and therefore all the subscribers. EventArgs does get round that for
you. For futher information on this argument and events in general, there was
recently a dotnetrocks episode on them:
http://www.dotnetrocks.com/default.aspx?showNum=355

I will leave it to you to come up with the solution yourself. Just thought I
would make you aware of the two sides to the debate.
--
Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com
"Swappy" wrote:

任何人都可以建议我传递除两个参数以外的更多参数

事件,如下所示?


事件:

Onbutton_click(对象发送者,EventArgs e)"


事件处理程序:

按钮.Click + = new EventHandler(Onbutton_click);


我想传递与该事件相关的更多信息。 &安培;想在活动中使用那个

的信息。
Can anyone suggest me to pass more parameters other than two parameter for
events like the following?

Event:
Onbutton_click(object sender, EventArgs e)"

Event handler:
button.Click += new EventHandler(Onbutton_click);

I want to pass more information related that event. & want to use that
information in the event.


这篇关于如何在事件处理程序中传递两个以上的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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