6.11事件
概念:按钮单击或者菜单选择时触发
例:
namespace ConsoleApplication1 { public delegate void mydelegate2(int newColor, int oldColor); //声明一个事件委托类型 class Shape { public event mydelegate2 ColorChange; //声明一个事件 public void fun(int x,int y) {
if(ColorChange!=null) ColorChange( x , y);//触发事件 } } class Class2 { public static void Main(String[] args) { Shape s = new Shape(); s.ColorChange += new mydelegate2(CCHandler);//订阅事件 //s.ColorChange -= CCHandler; //删除已订阅事件 s.fun(1,9); //调用方法以触发事件 } static void CCHandler(int x, int y)//事件处理方法 { Console.WriteLine("颜色从{0}变成{1}",x,y); Console.ReadLine(); } } }
浙公网安备 33010602011771号