如何定义事件

做了个小例子来说明如何定义事件和触发事件
  1 using System;
  2 using System.Drawing;
  3 using System.Collections;
  4 using System.ComponentModel;
  5 using System.Windows.Forms;
  6 using System.Data;
  7 
  8 namespace WA_TestEvent
  9 {
 10     /// <summary>
 11     /// Form1 的摘要说明。
 12     /// </summary>
 13     public class Form1 : System.Windows.Forms.Form
 14     {
 15         private System.Windows.Forms.Button button1;
 16         private System.Windows.Forms.TextBox textBox1;
 17         /// <summary>
 18         /// 必需的设计器变量。
 19         /// </summary>
 20         private System.ComponentModel.Container components = null;
 21 
 22         public Form1()
 23         {
 24             //
 25             // Windows 窗体设计器支持所必需的
 26             //
 27             InitializeComponent();
 28 
 29             //
 30             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 31             //
 32         }
 33 
 34         /// <summary>
 35         /// 清理所有正在使用的资源。
 36         /// </summary>
 37         protected override void Dispose( bool disposing )
 38         {
 39             if( disposing )
 40             {
 41                 if (components != null
 42                 {
 43                     components.Dispose();
 44                 }
 45             }
 46             base.Dispose( disposing );
 47         }
 48 
 49         #region Windows 窗体设计器生成的代码
 50         /// <summary>
 51         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 52         /// 此方法的内容。
 53         /// </summary>
 54         private void InitializeComponent()
 55         {
 56             this.button1 = new System.Windows.Forms.Button();
 57             this.textBox1 = new System.Windows.Forms.TextBox();
 58             this.SuspendLayout();
 59             // 
 60             // button1
 61             // 
 62             this.button1.Location = new System.Drawing.Point(96120);
 63             this.button1.Name = "button1";
 64             this.button1.TabIndex = 0;
 65             this.button1.Text = "负数触发事件";
 66             this.button1.Click += new System.EventHandler(this.button1_Click);
 67             // 
 68             // textBox1
 69             // 
 70             this.textBox1.Location = new System.Drawing.Point(8880);
 71             this.textBox1.Name = "textBox1";
 72             this.textBox1.TabIndex = 1;
 73             this.textBox1.Text = "0";
 74             // 
 75             // Form1
 76             // 
 77             this.AutoScaleBaseSize = new System.Drawing.Size(614);
 78             this.ClientSize = new System.Drawing.Size(292273);
 79             this.Controls.Add(this.textBox1);
 80             this.Controls.Add(this.button1);
 81             this.Name = "Form1";
 82             this.Text = "Form1";
 83             this.ResumeLayout(false);
 84 
 85         }
 86         #endregion
 87 
 88         /// <summary>
 89         /// 应用程序的主入口点。
 90         /// </summary>
 91         [STAThread]
 92         static void Main() 
 93         {
 94             Application.Run(new Form1());
 95         }
 96 
 97         private void button1_Click(object sender, System.EventArgs e)
 98         {
 99             int icount;
100             icount=Convert.ToInt32(textBox1.Text.Trim());
101             TestEventClass tec=new TestEventClass();
102             //给事件绑定处理函数,可以绑定多个处理函数
103             tec.TestEvent+= new TestEventHander(this.DoTestEvent);
104             tec.TestEvent+= new TestEventHander(this.DoTestEvent1);
105             tec.iCurrCount=icount;
106         }
107         //定义事件的处理函数,该函数的参数要和事件委托的参数一致
108         public void DoTestEvent(object sender,TestEventArgs e)
109         {
110             MessageBox.Show("你输入的值小于0:"+e.iCount.ToString(),"触发事件");
111         }
112         public void DoTestEvent1(object sender,TestEventArgs e)
113         {
114             MessageBox.Show("本事件的第二个处理函数","触发事件");
115         }
116     }
117 
118 
119     
120     //////////////////////当接收到负数时触发事件////////////////////////////////
121     
122 
123 
124     //定义事件委托
125     public delegate void TestEventHander(object sender,TestEventArgs e);
126 
127     //定义事件数据类,如果该事件不需要任何数据可以直接使用EventArgs类
128     public class TestEventArgs : System.EventArgs 
129     {
130         private int _iCount;
131         public int iCount
132         {
133             set
134             {
135                 _iCount = value;
136             }
137             get
138             {
139                 return _iCount;
140             }
141         }
142     }
143     //事件类
144     public class TestEventClass
145     {
146         //定义一个事件
147         public event TestEventHander TestEvent ;
148 
149         private int _iCurrtCount;
150         public int iCurrCount
151         {
152             get
153             {
154                 return _iCurrtCount;
155             }
156             set
157             {   //如果属性值小于0就触发事件
158                 _iCurrtCount=value;
159                 if (value < 0)
160                 {
161                     TestEventArgs e=new TestEventArgs();
162                     e.iCount=value;
163                     OnTestEvent(e);
164                 }
165 
166             }
167         }
168         //可重载,决定什么条件下出发事件
169         public virtual void OnTestEvent(TestEventArgs e)
170         {
171             if (TestEvent != null)//判断是否有处理函数
172                 TestEvent(this,e);//调用该事件的处理函数,相当于函数指针
173         }
174     }
175 
176 }
177 

posted on 2005-10-13 21:02  DoNet鸟  阅读(1571)  评论(0)    收藏  举报

导航