二刷:事件

1. 事件

image.png

1.1使用事件

  • 一个事件绑定两个时间处理器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace DelegateDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建时间拥有着
            Timer time1 = new Timer();
            //时间间隔1000ms
            time1.Interval = 1000;
            //事假响应者
            Boy boy = new Boy();
            Girl girl = new Girl();
            //绑定事件,事件订阅
            time1.Elapsed += boy.Action;
            time1.Elapsed += girl.Action;
            //开启事件
            time1.Start();
            Console.ReadLine();
        }
        public class Boy
        {
            //事假处理器
            internal void Action(object sender, ElapsedEventArgs e)
            {
                Console.WriteLine("jump!");
            }
        }

        public class Girl
        {
            internal void Action(object sender, ElapsedEventArgs e)
            {
                Console.WriteLine("sing");
            }
        }
    }

}
 

1.2一星事件应用

image.png
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace DelegateDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Form form1 = new Form();
            Controllor controller_01 = new Controllor(form1);
            form1.ShowDialog();
        }
    }

    class Controllor
    {
        private Form form;

        public Controllor(Form form)
        {
            if (form != null)
            {
                this.form = form;
                //时间绑定:点击窗体,显示当前时间
                this.form.Click += this.FormClicked;
            }
        }

        private void FormClicked(object sender, EventArgs e)
        {
            //事件
            this.form.Text = DateTime.Now.ToString();
            
        }
    }
}
 

1.3二星事件

image.png

namespace DelegateDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            MyForm form = new MyForm();
            //给事件自己绑定事件
            form.Click += form.FormCliked;
            form.ShowDialog();
        }
    }

    class MyForm : Form
    {
        internal void FormCliked(object sender, EventArgs e)
        {
            this.Text = DateTime.Now.ToString();
        }
    }
}

1.4三星事件

image.png
  • 点击按钮,文本框里面的文字变化
namespace formDemo
{
    public partial class MyForm : Form
    {
        public MyForm()
        {
            InitializeComponent();
            this.button1.Click += button1_Click;      
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "hello world!";
        }

1.5自定义事件

事件都是依赖于委托的

  • 完整格式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //事件拥有这
            Customer customer = new Customer();
            //事件响应者
            Waiter waiter = new Waiter();
            //事件订阅
            customer.Order += waiter.Action;
            customer.Action();
            customer.PayTheBill();

        }
    }
    //传递事件参数的类
    public class OrderEventArgs:EventArgs
    {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

    public delegate void OrderEventHandler(Customer customer, OrderEventArgs e);

    public class Customer
    {
        private OrderEventHandler orderEventHandler;
        //声明事件
        public event OrderEventHandler Order { 
            add
            {
                this.orderEventHandler += value;
            }
            remove
            {
                this.orderEventHandler -= value;
            }
        }
        public double Bill { get; set; }
        public void PayTheBill()
        {
            Console.WriteLine("I wall Pay ${0} ", this.Bill);
        }

        public void WalkIn()
        {
            Console.WriteLine("walk into a restaurant");
        }
        public void SitDown()
        {
            Console.WriteLine("sit down");
        }
        public void Think()
        {
            for (int i = 1; i < 5; i++)
            {
                Console.WriteLine("Let me think..");
                Thread.Sleep(1000);
            }
            if (this.orderEventHandler !=null)
            {
                OrderEventArgs e = new OrderEventArgs();
                e.DishName = "chicken";
                e.Size = "large";

                this.orderEventHandler.Invoke(this, e);
            }
        }
        public void Action()
        {
            this.WalkIn();
            this.SitDown();
            this.Think();
            Console.ReadLine();
        }
    }
    //声明事件响应者
    public class Waiter
    {
        //事件处理器
        public void Action(Customer customer, OrderEventArgs e)
        {
            Console.WriteLine("I will serve you the dish - {0}", e.DishName);
            double price = 10;
            switch (e.Size)
            {
                case "small":
                    price = price * 0.5;
                    break;
                case "large":
                    price = price * 1.5;
                    break;
                default:
                    break;
            }
            customer.Bill += price;
        }
    }
}

你可能感兴趣的:(二刷:事件)