Visual C#事件與介面編程執行個體

來源:互聯網
上載者:User
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    //定義介面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();    //執行個體化一個介面
     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 void EventFired4()
  {
   label2.Text = "";
   label2.Text = "You pressed UP arrow key";
  }
  
  private void button1_Click(object sender,System.EventArgs e)
  {
   Application.Exit();
  }
 } 

public class EventClass1 : IEvents//EventClass1繼承介面IEvents
 {
  public event dele event1;//定義事件成員event1
  //當事件發生時
  public void FireEvent()
  {
   event1();//呼叫事件處理
  }
 } 

  public class EventClass2 : IEvents
 {
  public event dele event1;
  public void FireEvent()
  {
   event1();
  }
 }
 public class EventClass3 : IEvents
 {
  public event dele event1;
  public void FireEvent()
  {
   event1();
  }
 } 
public class EventClass4 : IEvents
 {
  public event dele event1;
  public void FireEvent()
  {
   event1();
  }
 }  
}
原始碼下載:/Files/chinhr/EVENT-INTERFACE.rar

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.