订阅不带事件处理程序的单击事件,并使用自定义参数



我想让单击上下文菜单项做点什么,所以我会做:

cmsItemOne.Click += new EventHandler(someFunction);

但是,我有一个只需要传入自定义参数的函数:

private void customFunction(string someText, int someNumber)

那么我怎样才能订阅cmsItemOne.ClickcustomFunction并传入参数。

例如,(不起作用(

string theString = "Hello world";
int theInt = 5;
cmsItemOne.Click += customFunction(theString, theInt);

使用闭包

string theString = "Hello world";
int theInt = 5;
cmsItemOne.Click += (s,e) => customFunction(theString, theInt);

如何:订阅和取消订阅事件(C# 编程指南(

可以使事件处理程序方法使用多个参数调用该函数。

cmsItemOne.Click += new EventHandler(someFunction);
private void buttonGenTool_Click(object sender, EventArgs e)
{
customFunction(someText, someNumber);
}
private void customFunction(string someText, int someNumber)
{
//Your function code;
}

最新更新