浅谈.net事件机制

  大家都有这样的经历:
  打开VS--〉新建应用程序(FORM1)--〉往里面挪按钮--〉双击按钮--〉写代码
  这看上去那么的自然,简单,可仔细想一下,窗口(FORM1)是一个类,按钮(Button)是另一个类,FORM1怎么知道按钮点击了?
  
  仔细看看代码,发现有如下代码:
1 None.gif dot.gif
2 None.gif this .button1.Click  +=   new  System.EventHandler( this .button1_Click);
3 None.gifdot.gif
4 None.gif
5 None.gif private   void  button1_Click( object  sender, System.EventArgs e)
6 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
7InBlock.gif        
8ExpandedBlockEnd.gif        }

  难道这就是传说中的事件委托?@_@
  button1的Click事件在Form1中注册了button1_Click这个方法,所以当button1点击的时候程序执行到了button1_Click下面。

  我对这个过程模拟了程序,

 1 None.gif using  System;
 2 None.gif
 3 ExpandedBlockStart.gifContractedBlock.gif /**/ /// <summary>
 4InBlock.gif/// 按钮类
 5ExpandedBlockEnd.gif/// </summary>

 6 None.gif class  Button
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 9InBlock.gif    /// 传送给点击事件接收者的一些信息
10ExpandedSubBlockEnd.gif    /// </summary>

11InBlock.gif    public class ButtonEventArgs:EventArgs
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        public string buttonId;
14InBlock.gif        public ButtonEventArgs(string buttonId)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            this.buttonId = buttonId;
17ExpandedSubBlockEnd.gif        }

18ExpandedSubBlockEnd.gif    }

19InBlock.gif    
20InBlock.gif    //定义的委托
21InBlock.gif    public delegate void ButtonEventHandler(object sender,ButtonEventArgs args);
22InBlock.gif
23InBlock.gif    //事件成员
24InBlock.gif    public event ButtonEventHandler Click;
25InBlock.gif
26InBlock.gif    //假设用户点击按钮可以直接进入这个方法,
27InBlock.gif    public void UserClick(string buttonId)
28ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
29InBlock.gif        ButtonEventArgs e = new ButtonEventArgs(buttonId);
30InBlock.gif        Console.WriteLine("我是按钮({0})",e.buttonId);
31InBlock.gif        OnClick(e);
32ExpandedSubBlockEnd.gif    }
          
33InBlock.gif 
34InBlock.gif    //该方法通知到事件登记对象(Click就想个变量,它指向了事件登记对象的回调方法,既Page里的On_Click)
35InBlock.gif    protected virtual void OnClick(ButtonEventArgs e)
36ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
37InBlock.gif        if(Click != null)
38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
39InBlock.gif            Click(this,e);
40ExpandedSubBlockEnd.gif        }

41ExpandedSubBlockEnd.gif    }

42ExpandedBlockEnd.gif}

43 None.gif
44 None.gif class  Form1
45 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
46InBlock.gif    //登记Button的Click事件
47InBlock.gif    public Form1(Button btn)
48ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
49InBlock.gif        btn.Click += new Button.ButtonEventHandler(On_Click);
50ExpandedSubBlockEnd.gif    }

51InBlock.gif
52InBlock.gif    //这个方法可以撤销Button的Click事件
53InBlock.gif    public void Unregister(Button btn)
54ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
55InBlock.gif        Button.ButtonEventHandler callback = new Button.ButtonEventHandler(On_Click);
56InBlock.gif        btn.Click -= callback;
57ExpandedSubBlockEnd.gif    }

58InBlock.gif
59InBlock.gif    public void On_Click(object sender,Button.ButtonEventArgs e)
60ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
61InBlock.gif        Console.WriteLine("\n按钮:{0}已经被点击,将发生些事情dot.gif.",e.buttonId);
62ExpandedSubBlockEnd.gif    }

63InBlock.gif
64ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
65InBlock.gif    /// 程序入点
66ExpandedSubBlockEnd.gif    /// </summary>

67InBlock.gif    public static void Main()
68ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
69InBlock.gif        Button btn = new Button();
70InBlock.gif        Form1 frm = new Form1(btn);
71InBlock.gif        btn.UserClick("btnNo4");
72ExpandedSubBlockEnd.gif    }

73ExpandedBlockEnd.gif}

运行结果:
我是按钮(btnNo4)
按钮:btnNo4已经被点击,将发生些事情....

可以看出在Form1的构造中登记了Button的Click事件并将邦定到了Form1的On_Click方法中,Form1就可以不用关,Button中的点击到底是怎么实现的。

转载于:https://www.cnblogs.com/QiuYun/archive/2007/09/01/877926.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值