事件详解1

创建事件:using System.Timer;

                Timer timer=new timer;

响应:time.interval=1000;(1000毫秒响应一次事件)

订阅:time.Elapsed +=xxx.Acation;(“ += ” 是订阅操作符)红色是处理器名字可以自己定,然后再加Alt+回车生成处理器

类三大成员:属性(存储数据)、方法(做事)、事件(通知别人)。

事件模型五个组成部分:

        1.事件的拥有者(event sorce)

        2.时间成员

        3.事件的响应者

        4.事件的处理器

        5.事件订阅(把事件拥有者和响应者关联起来)

练习1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;///引用里记得添加


namespace ConsoleApp12
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Form form = new Form();
            Controller control=new Controller(form);
            form.ShowDialog();
        }
    }
    class Controller
    {
        private Form form;
        public Controller(Form form)
        {
            if (form != null)
            {
                this.form = form;
                this.form.Click += this.FromClick;
            }
        }
        private void FromClick(object sender, EventArgs e)
        {
            this.form.Text = DateTime.Now.ToString();
        }
    }
}
练习2:自己的方法处理自己的事件

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

namespace ConsoleApp13
{
    internal class Program
    {
        static void Main(string[] args)
        {
            MyForms myForms = new MyForms();
            myForms.Click += myForms.Acation;
            myForms.ShowDialog();
        }
    }
    class MyForms : Form
    {
        internal void Acation(object sender, EventArgs e)
        {
            this.Text = DateTime.Now.ToString();
        }
    }

}
练习3:事件响应者是一个事件拥有者的成员字段

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

namespace ConsoleApp14
{
    internal class Program
    {
        static void Main(string[] args)
        {
            MyForm myForm = new MyForm();
            myForm.ShowDialog();      
        }

    }

class MyForm : Form {
        private TextBox TextBox;
        private Button button;
        public MyForm() { 
        this.TextBox = new TextBox();
        this.button = new Button(); 
        this.Controls.Add(this.TextBox);//在屏幕中显示出来
        this.Controls.Add(this.button);
            this.button.Text = "sayHello";
            this.button.Top = 100;//文本框距离窗口顶端的距离
            this.button.Click += Button_Click;
        }

        private void Button_Click(object sender, EventArgs e)
        {
            this.TextBox.Text = "1235464";
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值