Visual C#事件与接口编程实例
时间:2018-08-05 11:19:53来源:网络整理
牐牶芏喑跹c#的朋友对于事件与接口感到迷惑不解,不明白它们之间的关系,下面我就用实例来简单的分析讲解一下。
牐犑录,用event修饰符来代表一个事件,我们要创建一个C#事件必须按以下顺序来扫行:
牐1,创建或标识一个代表。比如下例中的
牐爌ublic delegate void dele(); //声明代表,delegate 关键字通知编译器 dele 是一个委托类型
牐2,创建一个包含事件处理代表,调用事件处理代表的方法的类,如下例
牐爌ublic class EventClass1 : IEvents //IEvents,是下面我们要讲一接口
牐爗
牐爌ublic event dele event1;//定义事件成员event1
牐爌ublic void FireEvent() //当事件发生时
牐爗
牐爀vent1(); //调用事件处理
牐爙
牐爙
牐燛ventClass1继承接口IEvents,以下后面的EventClass2~4,都是一样。
牐3,定义一个或多个把方法连接到事件的类
牐4,使用事件
牐4.1 定义事件响应方法,如下例中的
牐營Events id1 = new EventClass1();
牐4.2 使用所定义的构造函数创建一个包含事件的对象,如下例中的
牐爄d1.event1 += new dele(EventFired1);
牐4.3 触发事件,如下例中的
牐爄d1.FireEvent();
牐犗旅嫖颐抢纯纯唇涌,我们必须用interface来声明一个接口。接口声明可以声明零个或多个成员。接口的成员必须是方法、属性、事件或索引器。接口不能包含常数、字段、运算符、实例构造函数、析构函数或类型,也不能包含任何种类的静态成员。
牐犓有接口成员都隐式地具有 public 访问权限。接口成员声明包含任何修饰符属于编译时错误。具体地说,接口成员包含下列任何修饰符属于编译时错误:abstract、public、protected、internal、private、virtual、override 或 static。更多的信息请看msdn help://MS.VSCC/MS.MSDNVS.2052/csspec/html/vclrfcsharpspec_13_1.htm
牐犜谙旅娴睦子中,我们声明IEvents接口,一个方法FireEvent和一个事件event1
牐爌ublic interface IEvents
牐爗
牐爀vent dele event1; //定义事件
牐爒oid FireEvent();//定义接口
牐爙
牐犜诤竺娴腅ventClass1~4类是继承了接口IEvent,因此在这几个类中必须实现上述一个方法和一个事件。下面的实例可以帮助大家更好的理解。
牐犝馐且桓黾虻サ膚indows Forms,包含一个textbox,几个labels和一个button,在程序启动时焦点在textbox,捕捉键盘按下事件,除方向键外,我能过接口来触事方向键按下事件。
牐犗旅娴拇码是一个网上常见的例程,大家可以拷贝下来,保存为.cs文件,用CSC编译就行
牐牬码如下:
牐爑sing System;
牐爑sing System.Drawing;
牐爑sing System.Collections;
牐爑sing System.ComponentModel;
牐爑sing System.Windows.Forms;
牐爑sing System.Data;
牐爊amespace Events_Interfaces
牐爗
牐爌ublic delegate void dele();//声明代表 delegate 关键字通知编译器 dele 是一个委托类型
牐爌ublic interface IEvents //定义接口IEvents,包含方法FireEvent事件event1
牐爗
牐爀vent dele event1;
牐爒oid FireEvent();
牐爙
牐爌ublic class Form1 : System.Windows.Forms.Form
牐爗
牐爌rivate System.Windows.Forms.Label label1;
牐爌rivate System.Windows.Forms.TextBox textBox1;
牐爌rivate System.Windows.Forms.Label label2;
牐爌rivate System.Windows.Forms.Button button1;
牐爌rivate System.Windows.Forms.Label label3;
牐爌rivate System.ComponentModel.Container components =null;
牐爌ublic Form1()
牐爗
牐營nitializeComponent();
牐爙
牐爌rotected override void Dispose( bool disposing )
牐爗
牐爄f( disposing )
牐爗
牐爄f (components != null)
牐爗
牐燾omponents.Dispose();
牐爙
牐爙
牐燽ase.Dispose( disposing );
牐爙
牐#region Windows Form Designer generated code
牐爌rivate void InitializeComponent()
牐爗
牐爐his.textBox1 = new System.Windows.Forms.TextBox();
牐爐his.label1 = new System.Windows.Forms.Label();
牐爐his.button1 = new System.Windows.Forms.Button();
牐爐his.label2 = new System.Windows.Forms.Label();
牐爐his.label3 = new System.Windows.Forms.Label();
牐爐his.SuspendLayout();
牐爐his.textBox1.Location = new System.Drawing.Point(8, 80);
牐爐his.textBox1.Name = "textBox1";
牐爐his.textBox1.Size = new System.Drawing.Size(56,23);
牐爐his.textBox1.TabIndex = 1;
牐爐his.textBox1.Text = "";
牐爐his.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Key_Press);
牐爐his.label1.Location = new System.Drawing.Point(16, 16);
牐爐his.label1.Name = "label1";
牐爐his.label1.Size = new System.Drawing.Size(256,64);
牐爐his.label1.TabIndex = 0;
牐爐his.label1.Text = "Whenever you use the arrow keys inside the text box, Corresponding events will be" +"fired to display the label appropriately. Have a try!!";
牐爐his.button1.Location = new System.Drawing.Point(240, 112);
牐爐his.button1.Name = "button1";
牐爐his.button1.Size = new System.Drawing.Size(48,23);
牐爐his.button1.TabIndex = 3;
牐爐his.button1.Text = "Exit";
牐爐his.button1.Click += new System.EventHandler(this.button1_Click);
牐//
牐// label2
牐//
牐爐his.label2.Location = new System.Drawing.Point(88, 80);
牐爐his.label2.Name = "label2";
牐爐his.label2.Size = new System.Drawing.Size(184,23);
牐爐his.label2.TabIndex = 2;
牐爐his.label2.TextAlign =System.Drawing.ContentAlignment.MiddleCenter;
牐//
牐// label3
牐//
牐爐his.label3.Location = new System.Drawing.Point(8, 104);
牐爐his.label3.Name = "label3";
牐爐his.label3.Size = new System.Drawing.Size(64,23);
牐爐his.label3.TabIndex = 4;
牐爐his.label3.TextAlign =System.Drawing.ContentAlignment.MiddleCenter;
牐//
牐// Form1
牐//
牐爐his.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
牐爐his.ClientSize = new System.Drawing.Size(292,141);
牐爐his.Controls.AddRange(new System.Windows.Forms.Control[]{this.label3,this.button1,this.label2,this.textBox1,this.label1});
牐爐his.Font= new System.Drawing.Font("Comic燬ansMS",8.25F,System.Drawing.FontStyle.Regular,
牐燬ystem.Drawing.GraphicsUnit.Point,((System.Byte)(0)));
牐爐his.Name = "Form1";
牐爐his.Text = "Events";
牐爐his.ResumeLayout(false);
牐爙
牐#endregion
牐爏tatic void Main()
牐爗
牐燗pplication.Run(new Form1());
牐爙
牐爌rivate void Key_Press(object sender,
牐燬ystem.Windows.Forms.KeyEventArgs e)
牐爗
牐爐extBox1.Text = "";
牐爈abel2.Text = "";
牐爏tring keyId = e.KeyCode.ToString();
牐爏witch (keyId)//判断是否按下方向键
牐爗
牐燾ase "Right":
牐爈abel3.Text = "";
牐營Events id1 = new EventClass1(); //实例化一个接口
牐爄d1.event1 += new dele(EventFired1);//定义EventClass1中的事件响应方法
牐爄d1.FireEvent();//调用EventClass1中的FireEvent方法,触发event1 事件,事件调用EventFired1方法
牐燽reak;
牐燾ase "Left":
牐爈abel3.Text = "";
牐營Events id2 = new EventClass2();
牐爄d2.event1 += new
牐燿ele(EventFired2);
牐爄d2.FireEvent();
牐燽reak;
牐燾ase "Down":
牐爈abel3.Text = "";
牐營Events id3 = new EventClass3();
牐爄d3.event1 += new
牐燿ele(EventFired3);
牐爄d3.FireEvent();
牐燽reak;
牐燾ase "Up":
牐爈abel3.Text = "";
牐營Events id4 = new EventClass4();
牐爄d4.event1 += new
牐燿ele(EventFired4);
牐爄d4.FireEvent();
牐燽reak;
牐燿efault:
牐爈abel3.Text = keyId;
牐燽reak;
牐爙
牐爙
牐//EventFired1方法
牐爌ublic void EventFired1()
牐爗
牐爈abel2.Text = "";
牐爈abel2.Text = "You pressed RIGHT arrow key";
牐爙
牐爌ublic void EventFired2()
牐爗
牐爈abel2.Text = "";
牐爈abel2.Text = "You pressed LEFT arrow key";
牐爙
牐爌ublic void EventFired3()
牐爗
牐爈abel2.Text = "";
牐爈abel2.Text = "You pressed DOWN arrow key";
牐爙
牐爌ublic void EventFired4()
牐爗
牐爈abel2.Text = "";
牐爈abel2.Text = "You pressed UP arrow key";
牐爙
牐爌rivate void button1_Click(object sender,
牐燬ystem.EventArgs e)
牐爗
牐燗pplication.Exit();
牐爙
牐爙
牐爌ublic class EventClass1 : IEvents
牐爗
牐爌ublic event dele event1;
牐爌ublic void FireEvent()
牐爗
牐爀vent1();
牐爙
牐爙
牐爌ublic class EventClass2 : IEvents
牐爗
牐爌ublic event dele event1;
牐爌ublic void FireEvent()
牐爗
牐爀vent1();
牐爙
牐爙
牐爌ublic class EventClass3 : IEvents
牐爗
牐爌ublic event dele event1;
牐爌ublic void FireEvent()
牐爗
牐爀vent1();
牐爙
牐爙
牐爌ublic class EventClass4 : IEvents//EventClass1继承接口IEvents
牐爗
牐爌ublic event dele event1;//定义事件成员event1
牐//当事件发生时
牐爌ublic void FireEvent()
牐爗
牐爀vent1();//调用事件处理
牐爙
牐爙
牐爙
牐犑录,用event修饰符来代表一个事件,我们要创建一个C#事件必须按以下顺序来扫行:
牐1,创建或标识一个代表。比如下例中的
牐爌ublic delegate void dele(); //声明代表,delegate 关键字通知编译器 dele 是一个委托类型
牐2,创建一个包含事件处理代表,调用事件处理代表的方法的类,如下例
牐爌ublic class EventClass1 : IEvents //IEvents,是下面我们要讲一接口
牐爗
牐爌ublic event dele event1;//定义事件成员event1
牐爌ublic void FireEvent() //当事件发生时
牐爗
牐爀vent1(); //调用事件处理
牐爙
牐爙
牐燛ventClass1继承接口IEvents,以下后面的EventClass2~4,都是一样。
牐3,定义一个或多个把方法连接到事件的类
牐4,使用事件
牐4.1 定义事件响应方法,如下例中的
牐營Events id1 = new EventClass1();
牐4.2 使用所定义的构造函数创建一个包含事件的对象,如下例中的
牐爄d1.event1 += new dele(EventFired1);
牐4.3 触发事件,如下例中的
牐爄d1.FireEvent();
牐犗旅嫖颐抢纯纯唇涌,我们必须用interface来声明一个接口。接口声明可以声明零个或多个成员。接口的成员必须是方法、属性、事件或索引器。接口不能包含常数、字段、运算符、实例构造函数、析构函数或类型,也不能包含任何种类的静态成员。
牐犓有接口成员都隐式地具有 public 访问权限。接口成员声明包含任何修饰符属于编译时错误。具体地说,接口成员包含下列任何修饰符属于编译时错误:abstract、public、protected、internal、private、virtual、override 或 static。更多的信息请看msdn help://MS.VSCC/MS.MSDNVS.2052/csspec/html/vclrfcsharpspec_13_1.htm
牐犜谙旅娴睦子中,我们声明IEvents接口,一个方法FireEvent和一个事件event1
牐爌ublic interface IEvents
牐爗
牐爀vent dele event1; //定义事件
牐爒oid FireEvent();//定义接口
牐爙
牐犜诤竺娴腅ventClass1~4类是继承了接口IEvent,因此在这几个类中必须实现上述一个方法和一个事件。下面的实例可以帮助大家更好的理解。
牐犝馐且桓黾虻サ膚indows Forms,包含一个textbox,几个labels和一个button,在程序启动时焦点在textbox,捕捉键盘按下事件,除方向键外,我能过接口来触事方向键按下事件。
牐犗旅娴拇码是一个网上常见的例程,大家可以拷贝下来,保存为.cs文件,用CSC编译就行
牐牬码如下:
牐爑sing System;
牐爑sing System.Drawing;
牐爑sing System.Collections;
牐爑sing System.ComponentModel;
牐爑sing System.Windows.Forms;
牐爑sing System.Data;
牐爊amespace Events_Interfaces
牐爗
牐爌ublic delegate void dele();//声明代表 delegate 关键字通知编译器 dele 是一个委托类型
牐爌ublic interface IEvents //定义接口IEvents,包含方法FireEvent事件event1
牐爗
牐爀vent dele event1;
牐爒oid FireEvent();
牐爙
牐爌ublic class Form1 : System.Windows.Forms.Form
牐爗
牐爌rivate System.Windows.Forms.Label label1;
牐爌rivate System.Windows.Forms.TextBox textBox1;
牐爌rivate System.Windows.Forms.Label label2;
牐爌rivate System.Windows.Forms.Button button1;
牐爌rivate System.Windows.Forms.Label label3;
牐爌rivate System.ComponentModel.Container components =null;
牐爌ublic Form1()
牐爗
牐營nitializeComponent();
牐爙
牐爌rotected override void Dispose( bool disposing )
牐爗
牐爄f( disposing )
牐爗
牐爄f (components != null)
牐爗
牐燾omponents.Dispose();
牐爙
牐爙
牐燽ase.Dispose( disposing );
牐爙
牐#region Windows Form Designer generated code
牐爌rivate void InitializeComponent()
牐爗
牐爐his.textBox1 = new System.Windows.Forms.TextBox();
牐爐his.label1 = new System.Windows.Forms.Label();
牐爐his.button1 = new System.Windows.Forms.Button();
牐爐his.label2 = new System.Windows.Forms.Label();
牐爐his.label3 = new System.Windows.Forms.Label();
牐爐his.SuspendLayout();
牐爐his.textBox1.Location = new System.Drawing.Point(8, 80);
牐爐his.textBox1.Name = "textBox1";
牐爐his.textBox1.Size = new System.Drawing.Size(56,23);
牐爐his.textBox1.TabIndex = 1;
牐爐his.textBox1.Text = "";
牐爐his.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Key_Press);
牐爐his.label1.Location = new System.Drawing.Point(16, 16);
牐爐his.label1.Name = "label1";
牐爐his.label1.Size = new System.Drawing.Size(256,64);
牐爐his.label1.TabIndex = 0;
牐爐his.label1.Text = "Whenever you use the arrow keys inside the text box, Corresponding events will be" +"fired to display the label appropriately. Have a try!!";
牐爐his.button1.Location = new System.Drawing.Point(240, 112);
牐爐his.button1.Name = "button1";
牐爐his.button1.Size = new System.Drawing.Size(48,23);
牐爐his.button1.TabIndex = 3;
牐爐his.button1.Text = "Exit";
牐爐his.button1.Click += new System.EventHandler(this.button1_Click);
牐//
牐// label2
牐//
牐爐his.label2.Location = new System.Drawing.Point(88, 80);
牐爐his.label2.Name = "label2";
牐爐his.label2.Size = new System.Drawing.Size(184,23);
牐爐his.label2.TabIndex = 2;
牐爐his.label2.TextAlign =System.Drawing.ContentAlignment.MiddleCenter;
牐//
牐// label3
牐//
牐爐his.label3.Location = new System.Drawing.Point(8, 104);
牐爐his.label3.Name = "label3";
牐爐his.label3.Size = new System.Drawing.Size(64,23);
牐爐his.label3.TabIndex = 4;
牐爐his.label3.TextAlign =System.Drawing.ContentAlignment.MiddleCenter;
牐//
牐// Form1
牐//
牐爐his.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
牐爐his.ClientSize = new System.Drawing.Size(292,141);
牐爐his.Controls.AddRange(new System.Windows.Forms.Control[]{this.label3,this.button1,this.label2,this.textBox1,this.label1});
牐爐his.Font= new System.Drawing.Font("Comic燬ansMS",8.25F,System.Drawing.FontStyle.Regular,
牐燬ystem.Drawing.GraphicsUnit.Point,((System.Byte)(0)));
牐爐his.Name = "Form1";
牐爐his.Text = "Events";
牐爐his.ResumeLayout(false);
牐爙
牐#endregion
牐爏tatic void Main()
牐爗
牐燗pplication.Run(new Form1());
牐爙
牐爌rivate void Key_Press(object sender,
牐燬ystem.Windows.Forms.KeyEventArgs e)
牐爗
牐爐extBox1.Text = "";
牐爈abel2.Text = "";
牐爏tring keyId = e.KeyCode.ToString();
牐爏witch (keyId)//判断是否按下方向键
牐爗
牐燾ase "Right":
牐爈abel3.Text = "";
牐營Events id1 = new EventClass1(); //实例化一个接口
牐爄d1.event1 += new dele(EventFired1);//定义EventClass1中的事件响应方法
牐爄d1.FireEvent();//调用EventClass1中的FireEvent方法,触发event1 事件,事件调用EventFired1方法
牐燽reak;
牐燾ase "Left":
牐爈abel3.Text = "";
牐營Events id2 = new EventClass2();
牐爄d2.event1 += new
牐燿ele(EventFired2);
牐爄d2.FireEvent();
牐燽reak;
牐燾ase "Down":
牐爈abel3.Text = "";
牐營Events id3 = new EventClass3();
牐爄d3.event1 += new
牐燿ele(EventFired3);
牐爄d3.FireEvent();
牐燽reak;
牐燾ase "Up":
牐爈abel3.Text = "";
牐營Events id4 = new EventClass4();
牐爄d4.event1 += new
牐燿ele(EventFired4);
牐爄d4.FireEvent();
牐燽reak;
牐燿efault:
牐爈abel3.Text = keyId;
牐燽reak;
牐爙
牐爙
牐//EventFired1方法
牐爌ublic void EventFired1()
牐爗
牐爈abel2.Text = "";
牐爈abel2.Text = "You pressed RIGHT arrow key";
牐爙
牐爌ublic void EventFired2()
牐爗
牐爈abel2.Text = "";
牐爈abel2.Text = "You pressed LEFT arrow key";
牐爙
牐爌ublic void EventFired3()
牐爗
牐爈abel2.Text = "";
牐爈abel2.Text = "You pressed DOWN arrow key";
牐爙
牐爌ublic void EventFired4()
牐爗
牐爈abel2.Text = "";
牐爈abel2.Text = "You pressed UP arrow key";
牐爙
牐爌rivate void button1_Click(object sender,
牐燬ystem.EventArgs e)
牐爗
牐燗pplication.Exit();
牐爙
牐爙
牐爌ublic class EventClass1 : IEvents
牐爗
牐爌ublic event dele event1;
牐爌ublic void FireEvent()
牐爗
牐爀vent1();
牐爙
牐爙
牐爌ublic class EventClass2 : IEvents
牐爗
牐爌ublic event dele event1;
牐爌ublic void FireEvent()
牐爗
牐爀vent1();
牐爙
牐爙
牐爌ublic class EventClass3 : IEvents
牐爗
牐爌ublic event dele event1;
牐爌ublic void FireEvent()
牐爗
牐爀vent1();
牐爙
牐爙
牐爌ublic class EventClass4 : IEvents//EventClass1继承接口IEvents
牐爗
牐爌ublic event dele event1;//定义事件成员event1
牐//当事件发生时
牐爌ublic void FireEvent()
牐爗
牐爀vent1();//调用事件处理
牐爙
牐爙
牐爙
本类热门