c#中如何自定义事件

事件是对象发送的消息,以发信号通知操作的发生。操作可能是由用户交互(例如鼠标单击)引起的,也可能是由某些其他的程序逻辑触发的。引发事件的对象称为事件发送方。捕获事件并对其作出响应的对象叫做事件接收方。

在c#中定义事件是非常简单的。对于没有事件数据的事件,可以用内置的EventHandle委托定义事件。EventHandle委托定义如下

public delegate void EventHandler(Object sender, EventArgs e);

声明事件用event关键字:

public event EventHandler myenvent;

1、对于有事件数据的自定义事件,需要派生EventArgs类的子类,处理事件数据,并自定义委托。实例如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace EVT
{
    public delegate void sayHello(object send, MyEventArg e);
    class Program
    {

        static void Main(string[] args)
        {
            A a1 = new A();
            a1.shakeHandle();
            Console.ReadLine();
        }
    }
    public class A
    {
       public event sayHello myevent2;
        public event EventHandler myevent;
        public void shakeHandle()
        {
            Console.WriteLine("握手触发!");
            if (myevent != null)
            {
                myevent(this, EventArgs.Empty);
            }
            if (myevent2 != null)
            {
                MyEventArg e = new MyEventArg();
                e.Message = "leijc";
                myevent2(this, e);
            }
            Console.WriteLine("握手结束!");
        }
        public A()
        {
            myevent += new EventHandler(this.SayHelloTo);
            myevent2 += new sayHello(this.SayHelloTo2);
        }
        public void SayHelloTo(object sender,EventArgs e)
        {
            Console.WriteLine("Hello World!");
        }
        public void SayHelloTo2(object sender, MyEventArg e)
        {
            Console.WriteLine(e.Message + "Hello World 2!" );
        }
    }
    public class MyEventArg : EventArgs
    {
        private string message;
        public string  Message
        {
            get { return message; }
            set { message = value; }
        }
    }
}

//例子中定义了一个MyEventArg,传递事件参数

2、在.net framework2.0中利用泛型可以简单实现带事件数据的自定义事件,上面的示例稍加改动即可。

using System;
using System.Collections.Generic;
using System.Text;

namespace EVT
{
    class Program
    {

        static void Main(string[] args)
        {
            A a1 = new A();
            a1.shakeHandle();
            Console.ReadLine();
        }
    }
    public class A
    {
        public event EventHandler<MyEventArg> myevent2;
        public event EventHandler myevent;
        public void shakeHandle()
        {
            Console.WriteLine("握手触发!");
            if (myevent != null)
            {
                myevent(this, EventArgs.Empty);
            }
            if (myevent2 != null)
            {
                MyEventArg e = new MyEventArg();
                e.Message = "leijc";
                myevent2(this, e);
            }
            Console.WriteLine("握手结束!");
        }
        public A()
        {

            myevent += new EventHandler(this.SayHelloTo);
            myevent2 += new EventHandler<MyEventArg>(this.SayHelloTo2);
        }
        public void SayHelloTo(object sender,EventArgs e)
        {
            Console.WriteLine("Hello World!");
        }
        public void SayHelloTo2(object sender, MyEventArg e)
        {
            Console.WriteLine(e.Message + "Hello World 2!" );
        }
    }
    public class MyEventArg : EventArgs
    {
        private string message;
        public string  Message
        {
            get { return message; }
            set { message = value; }
        }
    }
}

 

原创文章如转载请注明:转载自 http://www.llh4.com/

原文链接:http://www.llh4.com/post/934.html

posted on 2010-09-29 22:10  网络小筑  阅读(814)  评论(0编辑  收藏  举报

导航