如何理解和实现c#事件

网上有很多关于事件是如何实现的,但看来看去都不是很明了。

有时候很多网站都转载相同的内容,千篇一律,所以自己打算好好研究一下这东东究竟如何实现。

Step 1 什么是事件

究竟什么是事件呢?想到事件,就让我想到911. 记得那段事件大家都在谈911事件。什么是911事件呢?就是飞机撞大楼这个动作导致了911事件的产生。但在c#中又如何理解呢。其实所谓事件,就是由某个对象发出的消息,这个消息标志着某个特定的行为发生了,或者某个特定的条件成立了。比如用户点击了鼠标,这一单击就会引起Windows给按钮消息处理程序发送一个消息,这就是OnClick事件。那个触发(raise)事件的对象称为事件的发送者(event sender),捕获并响应事件的对象称为事件的接收者(event receiver)。在winform里面,这个发送者就是我们单击的那个按钮,响应者或者叫接收者就是winform窗体。

下面就用winform和button来举例子

Step 2 定义button类

ContractedBlock.gif ExpandedBlockStart.gif Button
 1 using System;
 2 
 3 namespace EventHandler
 4 {
 5     /// <summary>
 6     /// 在namespace下声明委托
 7     /// </summary>
 8     /// <param name="sender">事件发送者对象</param>
 9     /// <param name="e">事件参数对象,此对象继承EventArgs,如果事件不带参数,可以不要</param>
10     /// 
11     public delegate void OnClickEventHandler(object sender, NewEventArgs e);
12 
13     public class Button
14     {
15         /// <summary>
16         /// 用Event关键字声明一个事件
17         /// </summary>
18         public event OnClickEventHandler Click;
19         /// <summary>
20         /// 实例化NewEventArgs
21         /// </summary>
22         private NewEventArgs e = new NewEventArgs();
23         
24         /// <summary>
25         /// 按钮Name属性
26         /// </summary>
27         private string buttonName = string.Empty;
28         public string Name
29         {
30             get { return buttonName; }
31             set { buttonName = value; }
32         }
33         /// <summary>
34         /// 当“按钮”被click了
35         /// </summary>
36         public void OnClick()
37         {
38             // 设置参数属性
39             e.ObjectName = Name;
40 
41             if (null != Click)
42             {
43                 // 将事件传给接受者
44                 // this代表发送者,即这个被单击的按钮
45                 Click(this, e);
46             }
47         }
48     }
49 }
50 

 Step 3定义参数类

ContractedBlock.gif ExpandedBlockStart.gif NewEventArgs
 1 using System;
 2 
 3 namespace EventHandler
 4 {
 5     public class NewEventArgs:EventArgs
 6     {
 7         /// <summary>
 8         /// 实现一个属性,用于测试,这里用来保存Object的名字
 9         /// </summary>
10         string objectName = string.Empty;
11         public string ObjectName
12         {
13             get { return objectName; }
14             set { objectName = value; }
15         }
16     }
17 }

Step 4 定义WinForm类

ContractedBlock.gif ExpandedBlockStart.gif WinForm
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Collections;
 5 
 6 namespace EventHandler
 7 {
 8     class WinForm
 9     {
10         /// <summary>
11         /// 用arraylist模仿winform的Controls
12         /// </summary>
13         public ArrayList controls = new ArrayList();
14 
15         /// <summary>
16         /// 构造函数里加载button
17         /// </summary>
18         public WinForm()
19         {
20             InitComponent();
21         }
22 
23         /// <summary>
24         /// 用InitComponent模仿winform的InitializeComponent
25         /// 在windows应用程序中,它是在一个form的partial类里实现的
26         /// </summary>
27         public void InitComponent()
28         {
29             Button button1 = new Button();
30             button1.Name = "first button";
31             // 用+=添加事件到事件队列里(用-=将事件从队列中移除)
32             button1.Click += new OnClickEventHandler(btnClick);
33 
34             Button button2 = new Button();
35             button2.Name = "second button";
36             button2.Click += new OnClickEventHandler(btnClick);
37 
38             controls.Add(button1);
39             controls.Add(button2);
40 
41         }
42 
43         /// <summary>
44         /// button的单击事件
45         /// </summary>
46         /// <param name="sender">发送者对象,这里指button</param>
47         /// <param name="e">参数对象继承EventArgs</param>
48         protected void btnClick(object sender, NewEventArgs e)
49         {
50             if (sender is Button)
51             {
52                 Console.WriteLine("button");
53             }
54             Console.WriteLine(e.ObjectName);
55         }
56     }
57 }

Step 5 运行测试

ContractedBlock.gif ExpandedBlockStart.gif Test
 1 using System;
 2 
 3 namespace EventHandler
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             // 模拟button单击后,发送WM_MouseClick消息给windows处理程序
10             WinForm form1 = new WinForm();
11             foreach (Button button in form1.controls)
12             {
13                 button.OnClick();
14             }
15             Console.Read();
16         }
17     }
18 }
19 

 Step 6 小结

测试结果:

button

first button

button

second button

现在觉得event其实就这样,关键是理解委托的概念,这里的button1.Click += new OnClickEventHandler(btnClick);和button2.Click += new OnClickEventHandler(btnClick);其实就是委托实例而已。其次要了解事件sender对象和receiver之间的关系。

最后希望能和大家共同探讨技术问题。

参考:

http://www.csharphelp.com/archives/archive253.html

转载于:https://www.cnblogs.com/matthew/archive/2008/08/19/1270671.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值