C#委托和事件

      C#中的事件,即event,其采用的是事件驱动方式来使用,以前处理事件类似的事情往往采用等待机制,为了等待某事件的发生,需要不断地检测某些变量,而事件驱动程序等待事件时,不占用资源。

      事件从一方面来讲就是改变,就如button按钮一样,点击产生click事件,其click事件发生改变,就会通过委托调用相应的处理方法。

      委托,从某种程度上来说,委托好比一个通道,一个事件的发生,可以通过委托传递给不同的接受者,只要接受者订阅了此事件,就用权利在事件被触发时产生不同的处理方式。委托好比第三方,是独立于对象之外的东西。

 例:两个窗体实现文本框的互相传值:

Form1:

namespace WindowsForms

{
    public delegate void myDelegate(string text);
    public partial class Form1 : Form
    {
        public event myDelegate deletForm1;
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            form.deletForm2 += new myDelegate(this.TextMethod); //订阅事件
            this.deletForm1 += new myDelegate(form.TextMethod); //订阅事件
            form.Show();
          
        }

      //触发事件deletForm1

        public void button2_Click(object sender, EventArgs e)
        {
            if (deletForm1!= null)
            {            
                deletForm1(textBox1.Text);
            }
        }

       //阅读Form2的deletForm2事件

            public void TextMethod(string text) {
            this.textBox1.Text = text;
        }
    }
}

Form2:

namespace WindowsForms
{
    public partial class Form2 : Form
    {
        public event myDelegate deletForm2;
        public Form2()
        {
            InitializeComponent();       
        }

 //触发事件deletForm1

        private void button1_Click(object sender, EventArgs e)
        {
            if(this.deletForm2!=null){
                deletForm2(this.textBox1.Text);
            }
        }

 //阅读Form1的deletForm1事件

        public void TextMethod(string text)
        {
            this.textBox1.Text = text;
           
        } 

    }
}

EventAgrs是包含事件数据的类的积累,用于传递的事件的细节,在许多自定义事件中要定义自己的事件参数;

自定义事件举例:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace EventMyself {   

  public delegate void MyDele(object sender, MyEventArgs e);    

class Student     {

        public event MyDele OnLearn;//声明事件

        public virtual void DoEvent(MyEventArgs e)         {

            if (OnLearn != null)             {

                OnLearn(this, e);//触发事件

            }

        }       

    }   

  public class MyEventArgs : EventArgs     {

        public int _book;

        public MyEventArgs(int book)         {

            this._book = book;

        }

    }

    class Teacher     {

        public void TeachBook(object sender, MyEventArgs e)//第一个参数其实这里传递的是对象的引用,实质只的是一个对象      

   {

            Console.WriteLine("I teach {0} books", e._book);     

    }

      public static void Main()         {

            Student stu = new Student();

            Teacher tea = new Teacher();

            MyEventArgs e = new MyEventArgs(3);

            stu.OnLearn += new MyDele(tea.TeachBook);//订阅事件

            stu.DoEvent(e);//触发事件         

            Console.Read();

        }

    }

}

 

 

转载于:https://www.cnblogs.com/liuyuchneg-software/p/3387733.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值