【学习笔记】事件如何使用
来源:互联网 发布:淘宝秒刷销量一天千单 编辑:程序博客网 时间:2024/05/01 02:21
经过学习,终于搞清了事件的使用,以下附上一个小例子
事件主要使用三个类:
1. 参数定义类testEventArgs:继承自EventArgs(虽然EventArgs什么也不做)
2. 事件发布类monitor
3. 事件侦听类Receiver
using System;using System.Collections.Generic;using System.Text;namespace EventLearn{ class testArgs:EventArgs { public string msg; public testArgs(string msg) { this.msg = msg; } } class monitor { public delegate void delegateHander(object sender, testArgs e); public event delegateHander pressKey; public void Run() { do { Console.WriteLine("Please press any key. Input 'exit' to finish the process!"); string input=Console.ReadLine(); if (input == "exit") break; pressKey(this,new testArgs(input)); } while(true); } } class Receiver { public Receiver(monitor Monitor) { Monitor.pressKey += new monitor.delegateHander(this.process); } public void process(object sender, testArgs e) { Console.WriteLine("You have input the string: {0}", e.msg); } }}
主程序如下:
using System;using System.Collections.Generic;using System.Text;namespace EventLearn{ class Program { static void Main(string[] args) { monitor Monitor = new monitor(); Receiver receiver = new Receiver(Monitor); Monitor.Run(); Console.ReadKey(); } }}
- 【学习笔记】事件如何使用
- android 学习笔记 如何使用menu菜单
- head first C#学习笔记:如何创建事件以及事件处理程序
- FLEX学习笔记----事件
- 事件监听-学习笔记
- 事件监听学习笔记
- jQuery学习笔记:事件
- 随机事件学习笔记
- JavaScript事件学习笔记
- DOM事件学习笔记
- Jquery事件学习笔记
- C#学习笔记 事件
- DOM事件学习笔记
- DOM事件学习笔记
- 事件分发学习笔记
- DOM事件学习笔记
- C#学习笔记-事件
- Qt学习笔记-----事件
- linux常用命令学习之sed命令
- div.onblur的两个解决方法
- 一种莫名的感觉
- 利用HtmlClient生成静态页面
- linux常用命令学习之awk命令
- 【学习笔记】事件如何使用
- androidlayout_weight讲解
- 网易笔试题目
- Socket对传输文件结尾的判定
- Re-installation failed due to differentapplication signatures.
- Android应用程序的代码混淆和签名
- 蓄水池算法
- 第四周实验报告1(计算三角形的周长面积)
- Java 程序员应该了解的 10 个面向对象设计原则