zoukankan      html  css  js  c++  java
  • 简单Event事件的组成

    class Program
        {
            static void Main(string[] args)
            {
                Form form = new Form();//form是事件的拥有者
                Controler controler = new Controler(form);//controler是事件的响应者
                form.ShowDialog();
                
                
            }
        }
        class Controler
        {
          private  Form form;
          public Controler(Form form)
          {
                if(form!=null)
                { 
                    this.form = form;
                this.form.Click += this.FormClick;//是事件订阅,click是事件
                }
          }
    
          private void FormClick(object sender, EventArgs e)//事件的处理器
          {
              this.form.Text = DateTime.Now.ToString();
          }
    
        }

      static void Main(string[] args)
            {
                Timer timer = new Timer();//事件的拥有者
                timer.Interval = 1000;
                Boy boy = new Boy();//事件的响应者
                Girl girl = new Girl();
                timer.Elapsed += boy.Action;//Elapsed是事件,订阅
                timer.Elapsed += girl.Action;//Elapsed是事件,订阅
                timer.Start();
                Console.ReadLine();
    
            }
    
            
        }
    
        class Boy
        {
    
            internal void Action(object sender, ElapsedEventArgs e)//事件的处理器
            {
                Console.WriteLine("Jump");
            }
        }
        class Girl
        {
    
            internal void Action(object sender, ElapsedEventArgs e)
            {
                Console.WriteLine("Sing");
            }
        }
    
    
     public Form1()
            {
                InitializeComponent();
                //this.button3.Click += this.ButtonClick;//挂接事件处理器
                //this.button3.Click += new EventHandler(ButtonClick);//第二种挂接事件处理器,事件基于委托的意思就是说
                this.button3.Click += delegate(object send, EventArgs e)//第三种挂接事件处理器
                {
                    this.textBox1.Text = "haha";
                };
                this.textBox1.Click += (object send, EventArgs e) =>//第四种lambda挂接事件处理器
                {
                    this.textBox1.Text = "hehe";
                };
            }
    
            private void ButtonClick(object sender, EventArgs e)
            {
                if(sender==this.button1)
                {
                    this.textBox1.Text = "Hello";
                }
                if (sender == this.button2)
                {
                    this.textBox1.Text = " world";
                }
                if(sender==this.button3)
                {
    
                    this.textBox1.Text = "my:ok";
                }
            }
  • 相关阅读:
    json web token 入门
    Mysql查询表注释和字段注释信息
    Nginx核心知识100讲学习笔记(陶辉):目录
    Kubernetes进阶实战读书笔记:网络存储
    Kubernetes进阶实战读书笔记:持久化存储卷(pv详解)
    Kubernetes进阶实战读书笔记:存储卷概述
    sybase
    Delphi 解决StrToDateTime()不是有效日期类型的问题
    delphi TStringList 用法详解
    看看Delphi中的列表(List)和泛型
  • 原文地址:https://www.cnblogs.com/1521681359qqcom/p/11223500.html
Copyright © 2011-2022 走看看