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