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";
}
}