• 简单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";
                }
            }
  • 相关阅读:
    一卦,测一年运气
    测一下我心中想的事
    一卦,测一下我心里想的事
    这一卦,学到了不少东西
    癸山丁向下卦(七运)
    起卦测我心里想的事
    现在的卦,越来越看不懂了
    luogu P2759 奇怪的函数 |二分答案
    luogu P2515 [HAOI2010]软件安装 |Tarjan+树上背包
    luogu P2343 宝石管理系统 |分块+堆
  • 原文地址:https://www.cnblogs.com/1521681359qqcom/p/11223500.html
Copyright © 2020-2023  润新知