程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Visual C#事件與接口編程實例

Visual C#事件與接口編程實例

編輯:C#入門知識

很多初學c#的朋友對於事件與接口感到迷惑不解,不明白它們之間的關系,下面我就用實例來簡單的分析講解一下。

事件,用event修飾符來代表一個事件,我們要創建一個C#事件必須按以下順序來掃行:

1,創建或標識一個代表。比如下例中的

public delegate void dele(); 聲明代表,delegate 關鍵字通知編譯器 dele 是一個委托類型

2,創建一個包含事件處理代表,調用事件處理代表的方法的類,如下例

public class EventClass1 : IEvents file://IEvents,是下面我們要講一接口
{
public event dele event1;定義事件成員event1
public void FireEvent() file://當事件發生時
{
event1(); 調用事件處理
}
}

EventClass1繼承接口IEvents,以下後面的EventClass2~4,都是一樣。

3,定義一個或多個把方法連接到事件的類

4,使用事件

4.1 定義事件響應方法,如下例中的

IEvents id1 = new EventClass1();

4.2 使用所定義的構造函數創建一個包含事件的對象,如下例中的

id1.event1 += new dele(EventFired1);

4.3 觸發事件,如下例中的

id1.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

public interface IEvents
{
event dele event1; 定義事件
void FireEvent();//定義接口
}

在後面的EventClass1~4類是繼承了接口IEvent,因此在這幾個類中必須實現上述一個方法和一個事件。下面的實例可以幫助大家更好的理解。

這是一個簡單的windows Forms,包含一個textbox,幾個labels和一個button,在程序啟動時焦點在textbox,捕捉鍵盤按下事件,除方向鍵外,我能過接口來觸事方向鍵按下事件。


下面的代碼是一個網上常見的例程,大家可以拷貝下來,保存為.cs文件,用CSC編譯就行

代碼如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Events_Interfaces
{
public delegate void dele();//聲明代表 delegate 關鍵字通知編譯器 dele 是一個委托類型
public interface IEvents file://定義接口IEvents,包含方法FireEvent事件event1
{
event dele event1;
void FireEvent();
}
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label3;

private System.ComponentModel.Container components =null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();

this.textBox1.Location = new System.Drawing.Point(8, 80);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(56,23);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Key_Press);

this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(256,64);
this.label1.TabIndex = 0;
this.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!!";

this.button1.Location = new System.Drawing.Point(240, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48,23);
this.button1.TabIndex = 3;
this.button1.Text = "Exit";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(88, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(184,23);
this.label2.TabIndex = 2;
this.label2.TextAlign =System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 104);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64,23);
this.label3.TabIndex = 4;
this.label3.TextAlign =System.Drawing.ContentAlignment.MiddleCenter;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
this.ClientSize = new System.Drawing.Size(292,141);
this.Controls.AddRange(new System.Windows.Forms.Control[]{this.label3,this.button1,this.label2,this.textBox1,this.label1});

this.Font= new System.Drawing.Font("ComicSansMS",8.25F,System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,((System.Byte)(0)));
this.Name = "Form1";
this.Text = "Events";
this.ResumeLayout(false);
}
#endregion

static void Main()
{
Application.Run(new Form1());
}

private void Key_Press(object sender,
System.Windows.Forms.KeyEventArgs e)
{
textBox1.Text = "";
label2.Text = "";
string keyId = e.KeyCode.ToString();
switch (keyId)//判斷是否按下方向鍵
{
case "Right":
label3.Text = "";
IEvents id1 = new EventClass1(); file://實例化一個接口
id1.event1 += new dele(EventFired1);//定義EventClass1中的事件響應方法
id1.FireEvent();//調用EventClass1中的FireEvent方法,觸發event1 事件,事件調用EventFired1方法
break;
case "Left":
label3.Text = "";
IEvents id2 = new EventClass2();
id2.event1 += new
dele(EventFired2);
id2.FireEvent();
break;
case "Down":
label3.Text = "";
IEvents id3 = new EventClass3();
id3.event1 += new
dele(EventFired3);
id3.FireEvent();
break;
case "Up":
label3.Text = "";
IEvents id4 = new EventClass4();
id4.event1 += new
dele(EventFired4);
id4.FireEvent();
break;
default:
label3.Text = keyId;
break;
}
}
//EventFired1方法
public void EventFired1()
{
label2.Text = "";
label2.Text = "You pressed RIGHT arrow key";
}
public void EventFired2()
{
label2.Text = "";
label2.Text = "You pressed LEFT arrow key";
}
public void EventFired3()
{
label2.Text = "";
label2.Text = "You pressed DOWN arrow key";
}
public v

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved