浅析Visual C#事件处理机制

原文地址来自 http://chenpeng.cnblogs.com/archive/2006/07/23/457567.html
任何进行过图形用户界面开发的编程人员都会知道事件的概念。当用户在使用程序的时候,用户必然要和程序进行一定的交互。比如当用户点击窗体上的一个按钮后,程序就会产生该按钮被点击的事件,并通过相应的事件处理函数来响应用户的操作。这样用户的直观感觉就是程序执行了我要求的任务了。当然,事件并不一定是在和用户交互的情况下才会产生的,系统的内部也会产生一些事件并请求处理的,比如时钟事件就是一个很好例子。不过要介绍C#中的事件处理机制(扩展到更广的范围便是整个.Net框架),我们首先得明白一个叫"委托"的概念。

  C#中的委托:

  委托,顾名思义,就是中间代理人的意思。C#中的委托允许你将一个对象中的方法传递给另一个能调用该方法的类的某个对象。你可以将类A中的一个方法m(被包含在某个委托中了)传递给一个类B,这样类B就能调用类A中的方法m了。同时,你还可以以静态(static)的方式或是实例(instance)的方式来传递该方法。所以这个概念和C++中的以函数指针为参数形式调用其他类中的方法的概念是十分类似的。

  委托的概念首先是在Visual J++中被提出来的,现在C#也应用了委托的概念,这也可谓是"拿来主义"吧。C#中的委托是通过继承System.Delegate中的一个类来实现的,下面是具体的步骤:

  1. 声明一个委托对象,其参数形式一定要和你想要包含的方法的参数形式一致。

  2. 定义所有你要定义的方法,其参数形式和第一步中声明的委托对象的参数形式必须相同。

  3. 创建委托对象并将所希望的方法包含在该委托对象中。

  4. 通过委托对象调用包含在其中的各个方法。

  以下的C#代码显示了如何运用以上的四个步骤来实现委托机制的:

 1 None.gif using  System;
 2 None.giffile: // 步骤1: 声明一个委托对象
 3 None.gif public   delegate   void  MyDelegate( string  input);
 4 None.gif
 5 None.giffile: // 步骤2::定义各个方法,其参数形式和步骤1中声明的委托对象的必须相同
 6 ExpandedBlockStart.gifContractedBlock.gif class  MyClass1 dot.gif {
 7ExpandedSubBlockStart.gifContractedSubBlock.gifpublic void delegateMethod1(string input)dot.gif{
 8InBlock.gifConsole.WriteLine(
 9InBlock.gif"This is delegateMethod1 and the input to the method is {0}",
10InBlock.gifinput);
11ExpandedSubBlockEnd.gif}

12ExpandedSubBlockStart.gifContractedSubBlock.gifpublic void delegateMethod2(string input)dot.gif{
13InBlock.gifConsole.WriteLine(
14InBlock.gif"This is delegateMethod2 and the input to the method is {0}",
15InBlock.gifinput);
16ExpandedSubBlockEnd.gif}

17ExpandedBlockEnd.gif}

18 None.gif
19 None.giffile: // 步骤3:创建一个委托对象并将上面的方法包含其中
20 ExpandedBlockStart.gifContractedBlock.gif class  MyClass2 dot.gif {
21ExpandedSubBlockStart.gifContractedSubBlock.gifpublic MyDelegate createDelegate()dot.gif{
22InBlock.gifMyClass1 c2=new MyClass1();
23InBlock.gifMyDelegate d1 = new MyDelegate(c2.delegateMethod1);
24InBlock.gifMyDelegate d2 = new MyDelegate(c2.delegateMethod2);
25InBlock.gifMyDelegate d3 = d1 + d2;
26InBlock.gifreturn d3;
27ExpandedSubBlockEnd.gif}

28ExpandedBlockEnd.gif}

29 None.gif
30 None.giffile: // 步骤4:通过委托对象调用包含在其中的方法
31 ExpandedBlockStart.gifContractedBlock.gif class  MyClass3 dot.gif {
32ExpandedSubBlockStart.gifContractedSubBlock.gifpublic void callDelegate(MyDelegate d,string input)dot.gif{
33InBlock.gifd(input);
34ExpandedSubBlockEnd.gif}

35ExpandedBlockEnd.gif}

36 ExpandedBlockStart.gifContractedBlock.gif class  Driver dot.gif {
37ExpandedSubBlockStart.gifContractedSubBlock.gifstatic void Main(string[] args)dot.gif{
38InBlock.gifMyClass2 c2 = new MyClass2();
39InBlock.gifMyDelegate d = c2.createDelegate();
40InBlock.gifMyClass3 c3 = new MyClass3();
41InBlock.gifc3.callDelegate(d,"Calling the delegate");
42ExpandedSubBlockEnd.gif}

43ExpandedBlockEnd.gif}


  C#中的事件处理函数:

  C#中的事件处理函数是一个具有特定参数形式的委托对象,其形式如下:

public delegate void MyEventHandler(object sender, MyEventArgs e);

  其中第一个参数(sender)指明了触发该事件的对象,第二个参数(e)包含了在事件处理函数中可以被运用的一些数据。上面的MyEventArgs类是从EventArgs类继承过来的,后者是一些更广泛运用的类,如MouseEventArgs类、ListChangedEventArgs类等的基类。对于基于GUI的事件,你可以运用这些更广泛的、已经被定义好了的类的对象来完成处理;而对于那些基于非GUI的事件,你必须要从EventArgs类派生出自己的类,并将所要包含的数据传递给委托对象。下面是一个简单的例子:

public class MyEventArgs EventArgs{
public string m_myEventArgumentdata;
}

  在事件处理函数中,你可以通过关键字event来引用委托对象,方法如下:

public event MyEventHandler MyEvent;

  现在,我们来创建两个类,通过这两个类我们可以知道C#完成事件处理的机制是如何工作的。在我们的实例中,A类将提供事件的处理函数,并在步骤3中创建委托对象同时将事件处理函数包含在其中,同上所述,事件处理函数的参数形式必须和委托对象的参数形式相一致。然后,A类将委托对象传递给B类。当B类中的事件被触发后,A类中的事件处理函数就相应的被调用了。下面是示例代码:

 1 None.gif using  System;
 2 None.gif // file:步骤1:声明委托对象
 3 None.gif public   delegate   void  MyHandler1( object  sender,MyEventArgs e);
 4 None.gif public   delegate   void  MyHandler2( object  sender,MyEventArgs e);
 5 None.gif
 6 None.gif // file:步骤2:创建事件处理函数的方法
 7 ExpandedBlockStart.gifContractedBlock.gif class  A dot.gif {
 8InBlock.gifpublic const string m_id="Class A";
 9ExpandedSubBlockStart.gifContractedSubBlock.gifpublic void OnHandler1(object sender,MyEventArgs e)dot.gif{
10InBlock.gifConsole.WriteLine("I am in OnHandler1 and MyEventArgs is {0}",
11InBlock.gife.m_id);
12ExpandedSubBlockEnd.gif}

13ExpandedSubBlockStart.gifContractedSubBlock.gifpublic void OnHandler2(object sender,MyEventArgs e)dot.gif{
14InBlock.gifConsole.WriteLine("I am in OnHandler2 and MyEventArgs is {0}",
15InBlock.gife.m_id);
16ExpandedSubBlockEnd.gif}

17InBlock.gif
18InBlock.gif//file:步骤3:创建委托对象,并事件处理函数包含在其中同时设置好将要触发事件的对象
19ExpandedSubBlockStart.gifContractedSubBlock.gifpublic A(B b)dot.gif{
20InBlock.gifMyHandler1 d1=new MyHandler1(OnHandler1);
21InBlock.gifMyHandler2 d2=new MyHandler2(OnHandler2);
22InBlock.gifb.Event1 +=d1;
23InBlock.gifb.Event2 +=d2;
24ExpandedSubBlockEnd.gif}

25ExpandedBlockEnd.gif}

26 None.gif
27 None.gif // file:步骤4:通过委托对象(也就是触发事件)来调用被包含的方法
28 ExpandedBlockStart.gifContractedBlock.gif class  B dot.gif {
29InBlock.gifpublic event MyHandler1 Event1;
30InBlock.gifpublic event MyHandler2 Event2;
31ExpandedSubBlockStart.gifContractedSubBlock.gifpublic void FireEvent1(MyEventArgs e)dot.gif{
32ExpandedSubBlockStart.gifContractedSubBlock.gifif(Event1 != null)dot.gif{
33InBlock.gifEvent1(this,e);
34ExpandedSubBlockEnd.gif}

35ExpandedSubBlockEnd.gif}

36ExpandedSubBlockStart.gifContractedSubBlock.gifpublic void FireEvent2(MyEventArgs e)dot.gif{
37ExpandedSubBlockStart.gifContractedSubBlock.gifif(Event2 != null)dot.gif{
38InBlock.gifEvent2(this,e);
39ExpandedSubBlockEnd.gif}

40ExpandedSubBlockEnd.gif}

41ExpandedBlockEnd.gif}

42 ExpandedBlockStart.gifContractedBlock.gif public   class  MyEventArgs EventArgs dot.gif {
43InBlock.gifpublic string m_id;
44ExpandedBlockEnd.gif}

45 ExpandedBlockStart.gifContractedBlock.gif public   class  Driver dot.gif {
46ExpandedSubBlockStart.gifContractedSubBlock.gifpublic static void Main()dot.gif{
47InBlock.gifB b= new B();
48InBlock.gifA a= new A(b);
49InBlock.gifMyEventArgs e1=new MyEventArgs();
50InBlock.gifMyEventArgs e2=new MyEventArgs();
51InBlock.gife1.m_id ="Event args for event 1";
52InBlock.gife2.m_id ="Event args for event 2";
53InBlock.gifb.FireEvent1(e1);
54InBlock.gifb.FireEvent2(e2); 
55ExpandedSubBlockEnd.gif}

56ExpandedBlockEnd.gif}

57 None.gif

  C#中的GUI的事件处理函数:

  完成GUI下的事件处理函数的基本方法和上面介绍的并没有什么多大区别,下面我们就通过上面的方法来完成一个简单的实例程序。该实例程序的主类MyForm类是从Form类继承过来的。通过观察整段代码和相关的注解,你可以发现我们并没有给它声明委托对象并通过event关键字来引用该委托对象,那是因为GUI控件早就帮我们做好了该项工作,其委托对象是System.EventHandler。然而,我们还是要为各个控件定义方法(也就是事件的处理函数)并将它们包含在创建好的委托对象(System.EventHandler)中。那样,在用户和程序进行交互的时候,相应的事件处理函数就会被触发。具体代码如下:
None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  MyForm Form dot.gif {
InBlock.gif
private Button m_nameButton;
InBlock.gif
private Button m_clearButton;
InBlock.gif
private Label m_nameLabel;
InBlock.gif
InBlock.gif
private Container m_components = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
public MyForm()dot.gif{
InBlock.gifinitializeComponents();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
private void initializeComponents()dot.gif{
InBlock.gifm_nameLabel
=new Label();
InBlock.gifm_nameButton 
= new Button();
InBlock.gifm_clearButton 
= new Button();
InBlock.gif
InBlock.gifSuspendLayout();
InBlock.gif
InBlock.gifm_nameLabel.Location
=new Point(16,16);
InBlock.gifm_nameLabel.Text
="Click NAME button, please";
InBlock.gifm_nameLabel.Size
=new Size(300,23);
InBlock.gif
InBlock.gifm_nameButton.Location
=new Point(16,120);
InBlock.gifm_nameButton.Size
=new Size(17623);
InBlock.gifm_nameButton.Text
="NAME";
InBlock.gif
//file:创建委托对象,包含方法并将委托对象赋给按钮的Click事件
InBlock.gif
m_nameButton.Click += new System.EventHandler(NameButtonClicked);
InBlock.gif
InBlock.gifm_clearButton.Location
=new Point(16,152);
InBlock.gifm_clearButton.Size
=new Size(176,23);
InBlock.gifm_clearButton.Text
="CLEAR";
InBlock.gif
//file:创建委托对象,包含方法并将委托对象赋给按钮的Click事件
InBlock.gif
m_clearButton.Click += new System.EventHandler(ClearButtonClicked);
InBlock.gif
InBlock.gif
this.ClientSize = new Size(292271);
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.Controls.AddRange(new Control[] dot.gif{m_nameLabel,
InBlock.gifm_nameButton,
ExpandedSubBlockEnd.gifm_clearButton}
);
InBlock.gif
this.ResumeLayout(false);
ExpandedSubBlockEnd.gif}

InBlock.gif
//file:定义方法(事件的处理函数),其参数形式必须和委托对象的一致
ExpandedSubBlockStart.gifContractedSubBlock.gif
private void NameButtonClicked(object sender, EventArgs e)dot.gif{
InBlock.gifm_nameLabel.Text
=
InBlock.gif
"My name is john, please click CLEAR button to clear it";
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
private void ClearButtonClicked(object sender,EventArgs e)dot.gif{
InBlock.gifm_nameLabel.Text
="Click NAME button, please";
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
public static void Main()dot.gif{
InBlock.gifApplication.Run(
new MyForm());
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}
 

转载于:https://www.cnblogs.com/zhang1016/archive/2006/09/12/501783.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值