创建事件: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";
}
}
}

被折叠的 条评论
为什么被折叠?



