C#学习笔记(7)事件
2023-03-14 10:26:41 时间
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace demo7 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } ColorEventArgs CEA; //声明一个对象CEA private void button1_Click(object sender, EventArgs e) { if (radioButton1.Checked == true) { CEA = new ColorEventArgs("红色"); } else if (radioButton2.Checked == true) { CEA = new ColorEventArgs("蓝色"); } else if (radioButton3.Checked == true) { CEA = new ColorEventArgs("黄色"); } else if (radioButton4.Checked == true) { CEA = new ColorEventArgs("绿色"); } ColorEvent CE = new ColorEvent(); //实例化类ColorEvent ColorRecognize CR = new ColorRecognize(); //实例化ColorRecognize类 //订阅事件 CE.CRecognize += new ColorEvent.ColorRecognizeEventHandler(CR.color_Recognize); CE.OnCRecognize(CEA); } public class ColorEvent //定义事件确发类 { //定义委托ColorRecognizeEventHandler public delegate void ColorRecognizeEventHandler(object sender, ColorEventArgs e); public event ColorRecognizeEventHandler CRecognize; //定义事件CRecognize public void OnCRecognize(ColorEventArgs e) { if (CRecognize != null) //判断事件CRecognize是否为空值 { CRecognize(this, e); } //引发事件 } } public class ColorRecognize //定义类ColorRecognize显示颜色消息 { public void color_Recognize(object sender, ColorEventArgs e) { MessageBox.Show(e.getColorName); } //弹出消息框 } public class ColorEventArgs : EventArgs //定义派生于EventArgs类ColorEventArgs { private string _colorname; //颜色的名称 public ColorEventArgs(string c) //构造函数 { _colorname = c; } public string getColorName //获取颜色名称 { get { return _colorname; } } } } }
相关文章
- 发现 Linux SpaceFM 文件管理器的威力
- 夯实数据智能底座 共筑数字经济发展“新基石”
- 五分钟教你使用console.log发布公司的招聘信息
- 微软发布 Windows 11 Beta 预览版 Build 22621.1250 和 22623.1250 (KB5023008)
- Windows 11 学院:在 Windows 11 Build 25290 上如何为文件管理器启用标签页拖拽支持
- 微软Windows 11 Dev 预览版 Build 25290 发布:带来新的开始菜单提醒角标
- 微软正用全屏通知提醒 Windows 10 用户免费升级 Windows 11,要点击 5 次才能退出
- Linux Mint 21.2 将于 6 月发布:改进登录屏幕,优化 Pix 图像管理程序等等
- 字节面试也会问SPI机制?
- 微软正式停售 Windows 10 产品密钥 / 许可证:继续提供 ISO 镜像下载,推荐用户升级 Windows 11
- 用复古电脑程序 Toy CPU 学习低级编程
- Windows 11 新功能泄露:现代音量合成器、实验工具和新的文件管理器
- 微软 Windows 11 文件资源管理器全新设计曝光:UI 迎来大修,整合 Microsoft 365
- 对比Pandas,学习PySpark大数据处理
- 微软宣布为Windows 11 21H2设备开启自动更新到22H2版本
- 互联网都在说降本增效,小红书技术团队是怎么做的?
- 微软将继续提供 Windows 10 ISO 镜像下载,即将停售产品密钥 / 许可证
- 微软宣布为Windows 11 21H2 设备开启自动更新到 22H2 版本
- 面试突击:Properties和Yml有什么区别?
- 微软 Windows 11 Beta 预览版 22621.1245 和 22623.1245 (KB5022358) 发布