事件绑定的三种方式
僅適用于VC#3.0以上。
代碼?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?表達(dá)式為?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 表達(dá)式實現(xiàn)事件綁定的方式,實際效率并不是很高,它還是會生成一個事件處理的方法然后再進(jìn)行綁定。?
轉(zhuǎn)載于:https://www.cnblogs.com/gexiaoliang/archive/2010/05/23/1742089.html
總結(jié)
- 上一篇: windbg调试命令2(!gle、g、p
- 下一篇: 我是学农业水利工程的,不喜欢喝酒应酬,求