C# 中委托和事件的例子

时间:2015-01-10 08:01:04
【文件属性】:

文件名称:C# 中委托和事件的例子

文件大小:33KB

文件格式:ZIP

更新时间:2015-01-10 08:01:04

delegate event

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DelegateAndEventDemo { public delegate void ClickEventHandler(object sender, EventArgs e); //声明一个代理 public class MyButton { public event ClickEventHandler ClickEvent; //声明一个事件 public void Click() { if (ClickEvent != null) { Console.WriteLine("MyButton:我被单击了"); ClickEvent(this, null); } } } public class MyForm { public MyButton myButton = new MyButton(); public MyForm() { myButton.ClickEvent += new ClickEventHandler(OnClickEvent); } void OnClickEvent(object sender, EventArgs e) { Console.WriteLine("MyForm:我知道你被单击了"); } } class Program { static void Main(string[] args) { MyForm form = new MyForm(); form.myButton.Click(); Console.ReadKey(); } } }


【文件预览】:
DelegateAndEventDemo
----DelegateAndEventDemo.sln(902B)
----DelegateAndEventDemo.suo(46KB)
----DelegateAndEventDemo()
--------DelegateAndEventDemo.csproj(2KB)
--------Program.cs(1KB)
--------Properties()
--------obj()
--------bin()

网友评论