C# 如何使用EventHandler?
我只是创建一个 ContextMenu..
在这一行,我不知道应该在第三个参数中放入什么(或者更好:我必须如何形成它-语法-):
(contextMenuStrip.Items[0] as System.Windows.Forms.ToolStripMenuItem).DropDownItems.Add(contextUnterMenuStrip.Items.Add(exe),null, HERE);
在“此处”我必须设置一个EventHandler
onClick
通过示例我得到了这个方法:
public void DoSomething()
{
//...
}
我怎样才能调用这个方法? (通过事件处理程序?)或者我是否必须创建一个类似以下的方法:
private void button_Click(object sender, RoutedEventArgs e)
{
//...
}
I'm just creating a ContextMenu..
At this line, I don't know what I shall put in the third param (or better: how I have to form it -syntaxly-):
(contextMenuStrip.Items[0] as System.Windows.Forms.ToolStripMenuItem).DropDownItems.Add(contextUnterMenuStrip.Items.Add(exe),null, HERE);
on 'HERE' I have to set an EventHandler
onClick
By Example I got this Method:
public void DoSomething()
{
//...
}
How could I call this Method? (Over the Eventhandler?) or do I have to make a Method like:
private void button_Click(object sender, RoutedEventArgs e)
{
//...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要“调用”该方法,而是获取其地址。这意味着省略
()
Don't "call" the method but take its address. Which means omitting the
()
正如您参见此处,回调必须具有以下内容原型:
所以你的方法 DoSomething 必须如下所示:
As you can see here, the callback has to have the following prototype:
So your method DoSomething has to look like:
您可以使用 Linq 库创建匿名事件处理程序并以这种方式调用您的方法。这可能是一种很好且快速的做某事的方法(特别是如果它只是一个测试项目)。但如果你开始广泛使用它,阅读它可能会变得困难。
示例如下:
有关使用 Linq 的更多信息,请参阅此处:http://msdn.microsoft .com/library/bb308959.aspx
You can create an anonymous event handler using the Linq libraries and call your method that way. This can be a nice and quick way of doing something (especially if it's just a test project). But if you start using it extensively, it might become difficult to read it.
An example of this would be:
Refer here for further information on using Linq: http://msdn.microsoft.com/library/bb308959.aspx