Jon Skeet 已经回答了这个问题,展示了如何使用 lamda 表达式,但我仍然不清楚。我仍然需要更多示例,最终使用按钮找到了这个简单的案例:http: //social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/74d03fe0-0fa5-438d-80e0-cf54fa15af0e
void A()
{
Popup parameter = new Popup();
buttonClose.Click += (sender, e) => { buttonClose_Click(sender, e, parameter); };
}
static void buttonClose_Click(object sender, EventArgs e, Popup parameter)
{
MakeSomethingWithPopupParameter(parameter);
}
在我的例子中,我使用的是 TreeView 控件的上下文菜单,最终看起来像这样:
private void TreeViewCreateContextMenu(TreeNode node)
{
ContextMenuStrip contextMenu = new ContextMenuStrip();
// create the menu items
ToolStripMenuItem newMenuItem = new ToolStripMenuItem();
newMenuItem.Text = "New...";
// add the menu items to the menu
contextMenu.Items.AddRange(new ToolStripMenuItem[] { newMenuItem });
// add its event handler using a lambda expression, passing
// the additional parameter "myData"
string myData = "This is the extra parameter.";
newMenuItem.Click += (sender, e) => { newMenuItem_Click(sender, e, myData); };
// finally, set the node's context menu
node.ContextMenuStrip = contextMenu;
}
// the custom event handler, with "extraData":
private void newMenuItem_Click(object sender, EventArgs e, string extraData)
{
// do something with "extraData"
}