如何把一个事件响应函数写成lambda表达示?
例如,我有下面的代码:
private void button1_Click(object sender, EventArgs e)
{
((Button)sender).Text = "clicked";
var b = new Button();
b.Text = "new";
b.Click += new EventHandler(my);
Controls.Add(b);
}
private void my(object sender, EventArgs e)
{
((Button)sender).Text = "clicked";
}
我想把my函数写成b.Click+=后面接一个lambda表达式,这样就不需要写my这个函数名了。如何写呢?
------解决方案--------------------本帖最后由 caozhy 于 2014-02-26 16:05:54 编辑
((Button)sender).Text = "clicked";
var b = new Button();
b.Text = "new";
b.Click += new EventHandler((a, c) => { ((Button)a).Text = "clicked"; });
Controls.Add(b);
------解决方案--------------------button1.Click += (o, i) =>
{
((Button)o).Text = "clicked";
};
}
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
------解决方案-------------------- b.Click += (x, y) => ((Button)x).Text = "clicked";
------解决方案--------------------button1.Click += new EventHandler((sender, e) => 你的代码);