日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

浅析C#的事件处理和自定义事件

發布時間:2024/7/19 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅析C#的事件处理和自定义事件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、簡單的自定義事件(1):無參數

namespace UserInputMonitor

{

??? class UserInputMonitor

??? {

??????? public delegate void UserRequest(object sender, EventArgs e);

??????? //定義委托

??????? public event UserRequest OnUserRequest;

??????? //此委托類型類型的事件

????? ??public void Run()

??????? {

??????????? bool finished = false;

??????????? do

??????????? {

??????????????? if (Console.ReadLine() == "h")

??????????????? {

??????????????????? OnUserRequest(this, new EventArgs());

??????????????? }

??????????????? else if (Console.ReadLine() == "e")

??????????????? {

??????????????????? finished = true;

??????????????? }

??????????? } while (!finished);

??????? }

??? }

??? public class Client

??? {

??????? public static void Main()

??????? {

??????????? UserInputMonitor monitor = new UserInputMonitor();

??????????? new Client(monitor);

??????????? monitor.Run();

??????? }

??????? private void ShowMessage(object sender, EventArgs e)

??????? {

??????????? Console.WriteLine("HaHa!!");

??????? }

??????? Client(UserInputMonitor m)

??????? {

??????????? m.OnUserRequest += new UserInputMonitor.UserRequest(this.ShowMessage);

??????????? //m.OnUserRequest+=new m.UserRequest(this.ShowMessage);

??????????? //注意這種寫法是錯誤的,因為委托是靜態的

??????? }

??? }

}

二、簡單的自定義事件(2):包含參數

class MyEventArgs:EventArgs

{

??? private string keyChars;

???

??? public MyEventArgs(string keyChars)

??? {

??????? this.keyChars = keyChars;

??? }

???

??? public string KeyChars

??? {

??????? get

??????? {

??????????? return keyChars;

??????? }

??? }

}

class UserInputMonitor

{

??? //定義委托

??? public delegate void UserRequest(object sender,MyEventArgs e);

??? //此委托類型類型的事件,用戶累加客戶端事件

??? public event UserRequest OnUserRequest;

??? public void Run() //定義此事件激發的條件

??? {

??????? bool finished=false;

??????? do

??????? {

?????????? string inputString= Console.ReadLine();

?????????? if (inputString != "")

?????????????? OnUserRequest(this, new MyEventArgs(inputString));

?????????? else

?????????????? finished = true;

??????? }while(!finished);

???? }

}

public class Client

{

???? public static void Main()

???? {

????????? UserInputMonitor monitor=new UserInputMonitor();

????????? new Client(monitor);

????????? monitor.Run();

???? }

???? private void ShowMessage(object sender,MyEventArgs e)

???? {

????????? Console.WriteLine("捕捉到:{0}",e.KeyChars);

???? }

???? Client(UserInputMonitor m)

???? {

????????? m.OnUserRequest+=new UserInputMonitor.UserRequest(this.ShowMessage);

????????? //m.OnUserRequest+=new m.UserRequest(this.ShowMessage);

????????? //注意這種寫法是錯誤的,因為委托是靜態的

???? }

}

轉載于:https://www.cnblogs.com/qqhfeng/archive/2010/02/26/1674102.html

總結

以上是生活随笔為你收集整理的浅析C#的事件处理和自定义事件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。