c#事件学习(展示Form窗口)

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

namespace ConsoleApp12
{
    class Program
    {
        static void Main(string[] args)
        {
            Form form = new Form();//事件拥有者
            Contorller contor = new Contorller(form);//事件响应者的实例
            form.ShowDialog();
        }
    }

    class Contorller
    {
        private Form form;//Form的字段

        public Contorller(Form form)//Contorller的构造函数(构造器)
        {
            if(form!=null)
            {
                this.form = form;
                form.Click += this.Form_Click;//事件处理器与事件的一种约定
            }
        }

        private void Form_Click(object sender, EventArgs e) //事件处理器
        {
            form.Text = DateTime.Now.ToString();
        }
    }
}


版权声明:本文为weixin_68861604原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。