仅适用于VC#3.0以上。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
using
System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace Demo
11 {
12 public partial class Form3 : Form
13 {
14
15 public Form3()
16 {
17 InitializeComponent();
18 // 为 btnFirst 绑定 Click 事件处理程序
19 this .btnFirst.Click += new EventHandler(Button_Click);
20 // 为 btnSecond 绑定 Click 事件处理程序 另一种形式
21 this .btnSecond.Click += Button_Click;
22 // 使用 lambda 表达式为 btnSecond 绑定 Click 事件处理程序
23 this .btnSecond.Click += (sender, e) => MessageBox.Show((sender as Button).Name + " is pressed! " );
24 }
25
26 private void Form3_Load( object sender, EventArgs e)
27 {
28
29 }
30
31 private void Button_Click( object sender, EventArgs e)
32 {
33 if ((sender as Button).Name == " btnFirst " )
34 {
35 MessageBox.Show( " btnFirst pressed! " );
36 }
37 else
38 {
39 MessageBox.Show( " btnSecond pressed! " );
40 }
41 }
42
43 }
44
45 }
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace Demo
11 {
12 public partial class Form3 : Form
13 {
14
15 public Form3()
16 {
17 InitializeComponent();
18 // 为 btnFirst 绑定 Click 事件处理程序
19 this .btnFirst.Click += new EventHandler(Button_Click);
20 // 为 btnSecond 绑定 Click 事件处理程序 另一种形式
21 this .btnSecond.Click += Button_Click;
22 // 使用 lambda 表达式为 btnSecond 绑定 Click 事件处理程序
23 this .btnSecond.Click += (sender, e) => MessageBox.Show((sender as Button).Name + " is pressed! " );
24 }
25
26 private void Form3_Load( object sender, EventArgs e)
27 {
28
29 }
30
31 private void Button_Click( object sender, EventArgs e)
32 {
33 if ((sender as Button).Name == " btnFirst " )
34 {
35 MessageBox.Show( " btnFirst pressed! " );
36 }
37 else
38 {
39 MessageBox.Show( " btnSecond pressed! " );
40 }
41 }
42
43 }
44
45 }
使用 lambda 表达式实现事件绑定的方式,实际效率并不是很高,它还是会生成一个事件处理的方法然后再进行绑定。