首页 > 其他 > 详细

C#学习笔记(6)事件

时间:2014-03-12 15:26:45      阅读:407      评论:0      收藏:0      [点我收藏+]

bubuko.com,布布扣

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; }
            }
        }
    }
}


C#学习笔记(6)事件,布布扣,bubuko.com

C#学习笔记(6)事件

原文:http://blog.csdn.net/jkxqj/article/details/21086863

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!