需要从tsMenuItem.Events中提取Click事件 [英] Need to extract the Click event from tsMenuItem.Events

查看:80
本文介绍了需要从tsMenuItem.Events中提取Click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

newItem和tsMenuItem是ToolStripMenuItems。


我想将tsMenuItem中的click事件添加到newItem。


可以'这样做是因为事件受到保护:


newItem.Events.AddHandlers(tsMenuItem.Events);


不能这样做因为点击必须在左侧:


newItem.Click + = tsMenuItem.Click;


有什么方法我可以提取点击来自tsMenuItem.Events的事件,所以

我可以这样做:


newItem.Click + = ??


提前致谢

newItem and tsMenuItem are ToolStripMenuItems.

I''d like to add the click event in tsMenuItem to newItem.

Can''t do this because Events is Protected:

newItem.Events.AddHandlers(tsMenuItem.Events);

Can''t do this because Click must be on the left side:

newItem.Click += tsMenuItem.Click;

Is there some way I can extract the Click event from tsMenuItem.Events so
that I can do this:

newItem.Click += ??

Thanks in advance

推荐答案

2008年4月1日星期二12:26:42 -0700,AAaron123< aa ****** *@roadrunner.com>

写道:
On Tue, 01 Apr 2008 12:26:42 -0700, AAaron123 <aa*******@roadrunner.com>
wrote:

newItem和tsMenuItem是ToolStripMenuItems。


我想将tsMenuItem中的click事件添加到newItem。
newItem and tsMenuItem are ToolStripMenuItems.

I''d like to add the click event in tsMenuItem to newItem.



所以你想要响应

来调用tsMenuItem.Click事件处理程序到newItem上的Click事件吗?


怎么样:


newItem.Click + = delegate(object sender,EventArgs e)

{tsMenuItem.PerformClick() ; };


Pete

So you want the tsMenuItem.Click event handlers to be called in response
to a Click event on newItem?

What about this:

newItem.Click += delegate(object sender, EventArgs e)
{ tsMenuItem.PerformClick(); };

Pete


好吧,现在我的代码集合无法正常运行和你的陈述

确实如此。


我对C#很新。你可以补充一些关于语法的说法以及它是如何工作的。


我不确定你是在定义一个Delagate还是声明一个。

非常感谢

" Peter Duniho" < Np ********* @nnowslpianmk.com在留言中写道

news:op *************** @ petes-computer.local ...

2008年4月1日星期二12:26:42 -0700,AAaron123< aa ******* @ roadrunner.com>

写道:
Well, now I have my collection of code that doesn''t work and your statement
that does.

I''m quite new to C#. Could you add a few words about the syntax and how it
works.

I not sure if you are defining a Delagate or declaring one.
Thanks very much
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 01 Apr 2008 12:26:42 -0700, AAaron123 <aa*******@roadrunner.com>
wrote:

newItem和tsMenuItem是ToolStripMenuItems。


我想将tsMenuItem中的click事件添加到newItem。
newItem and tsMenuItem are ToolStripMenuItems.

I''d like to add the click event in tsMenuItem to newItem.



所以你想要响应

来调用tsMenuItem.Click事件处理程序到newItem上的Click事件吗?


怎么样:


newItem.Click + = delegate(object sender,EventArgs e)

{tsMenuItem.PerformClick() ; };


Pete

So you want the tsMenuItem.Click event handlers to be called in response
to a Click event on newItem?

What about this:

newItem.Click += delegate(object sender, EventArgs e)
{ tsMenuItem.PerformClick(); };

Pete


2008年4月1日星期二15:04:35 -0700,AAaron123< aa ** *****@roadrunner.com>

写道:
On Tue, 01 Apr 2008 15:04:35 -0700, AAaron123 <aa*******@roadrunner.com>
wrote:

嗯,现在我有我的代码集合,但不是工作和你的

声明

确实如此。


我是C#的新手。你能补充一些关于语法的话吗?

它是什么?
有效。


我不确定你是否定义了Delagate或宣布一个。
Well, now I have my collection of code that doesn''t work and your
statement
that does.

I''m quite new to C#. Could you add a few words about the syntax and how
it
works.

I not sure if you are defining a Delagate or declaring one.



当然。在我看来,术语定义是指和声明没有足够明确的
。但是,在这种情况下,委托类型已经定义了

(它是Click事件使用的类型,即
EventHandler),并且您声明了匿名方法用于

创建添加到Click事件的委托实例。


顾名思义,它基本上是一种没有a的方法名称。您可以通过编写一个调用

" tsMenuItem.PerformClick()"的命名方法来完成相同的操作,然后将该命名添加到事件中。

例如,这样的方法:


void ClickRedirect(对象发送者,EventArgs e)

{

tsMenuItem.PerformClick();

}


然后在其他地方出现一行代码如下:


newItem.Click + = ClickRedirect;


使用匿名方法允许代码更自包含恕我直言,

但否则它''更多的是偏好问题,至少在这个特殊情况下是这样的。


Pete

Sure. It seems to me that the terms "defining" and "declaring" are not
sufficiently unambiguous. However, in this case the delegate type is
already defined (it''s the type used by the Click event, which is
EventHandler), and you are declaring an anonymous method to be used to
create the delegate instance added to the Click event.

As the name suggests, it''s basically a method without a name. You could
do the exact same thing by writing a named method that calls
"tsMenuItem.PerformClick()", and adding that named to the event instead.
For example, a method like this:

void ClickRedirect(object sender, EventArgs e)
{
tsMenuItem.PerformClick();
}

and then elsewhere a line of code that looks like:

newItem.Click += ClickRedirect;

Using the anonymous method allows the code to be more self-contained IMHO,
but otherwise it''s more a matter of preference, at least in this
particular situation.

Pete


这篇关于需要从tsMenuItem.Events中提取Click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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