All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP入门:在PHP关于变量的使用介绍_[PHP教程]
网页特效:在任何位置显示html菜单_JavaScript技术_编程技术
ADO.NET使用事务的使用实例
PHP将MYSQL内容读到二维数组并按指定列输出_[PHP教程]
菜鸟的最爱,.NET经典代码汇总(一)_.net资料_编程技术
ASP.NET Remoting体系结构(七)
用GridView编辑更新独立的单元格_[Asp.Net教程]
Web服务器IIS6的PHP最佳配置方法_php资料_编程技术
visual c++添加菜单命令处理函数
如何在十天内学会php之第一天_[PHP教程]
Reading number is top 10 pictures
A resort photographed beautiful young woman change clothes process vomiting blood2
刘亦菲写真集1
徐若瑄展示美丽胸围2
Group of female porn in 《westwards》, uninhibited woman threatened to not the bottom line1
The mother was a stay-at-home children too tired took three baby suicide
So beauty, will let you spray blood9
八个盛产美女的国家2
A man's favorite things11
Is said to be a Chinese female artist fame explicit pictures before2
Ashlynn Brooke photograph of a group1
Download software ranking
linux初级教程
C语言教程TXT
VC++6.0培训教程
Tram sex maniac 2 (H) rar bag9
中国结婚习俗实录
网络管理员第三版
Tram sex maniac 2 (H) rar bag11
Tram sex maniac 2 (H) rar bag5
Unix video tutorial19
徐若瑄成名作“魔鬼天使”
delv published in(发表于) 2014/1/6 9:06:20 Edit(编辑)
C#之委托和事件_[Asp.Net教程]

C#之委托和事件_[Asp.Net教程]

C#-委托和事件_[Asp.Net教程]

要为类构造一个事件,必须用 event 来声明一个 delegate 型的字段,如:

puclic calss Test{
public delegate EventHandler(object sender, EventArgs e); //声明为delegate 型的事件;
}


  然后要指定一个事件的名称,并写出处理语句:
public event EventHandler Load


  在创建类的实例后定义这个 “Load”事件:
Test m=new Test();
m.load=new EventHandler(m_Load);
void m_Load(object sender, EventArgs e)
{
MessageBox.Show(" this is a class event");
}


  再看看下面的完整的一段代码:


using System;

class TestClass
{
static void Main(string[] args)
{
EventClass myEventClass = new EventClass();
myEventClass.CustomEvent += new EventClass.CustomEventHandler(CustomEvent1); // 关联事件句柄;
myEventClass.CustomEvent += new EventClass.CustomEventHandler(CustomEvent2);
myEventClass.InvokeEvent();
myEventClass.CustomEvent -= new EventClass.CustomEventHandler(CustomEvent2);
myEventClass.InvokeEvent();
myEventClass.Load += new EventClass.CustomEventHandler(Load1);
myEventClass.onLoad();
}


private static void CustomEvent1(object sender, EventArgs e)
{
Console.WriteLine("Fire Event 1");
}


private static void CustomEvent2(object sender, EventArgs e)
{
Console.WriteLine("Fire Event 2");
}
private static void Load1(object sender, EventArgs e)
{
Console.WriteLine("Current background color is {0}. Please input:", System.Console.BackgroundColor.ToString());
}
}


public class EventClass
{
public delegate void CustomEventHandler(object sender, EventArgs e);//首先定义一个委托类型的对象CustomEventHandler


//用delegate数据类型声明事件,要用event关键字,这里定义了两个字件;
public event CustomEventHandler CustomEvent;
public event CustomEventHandler Load;

public void InvokeEvent()
{
CustomEvent(this, EventArgs.Empty);
}
public void onLoad()
{
Console.BackgroundColor = ConsoleColor.Red;
Load(this, EventArgs.Empty);
string s = Console.ReadLine();
if (s != "yuping")
{
Console.WriteLine("You must type 'yuping' for change it !");
}
else
{
Console.BackgroundColor = System.ConsoleColor.Black;
Console.Clear();
}


}
}


  在包含事件声明的类中,声明一个数据类型是委托的这么样的一个对象CustomEventHandler, 它有两个参数(sender和e);在这里使用委托的目的就是在运行中向参数传递方法,并由委托对象生成的实例接收这个参数方法的返回值,因此,在声明委托型的对象时应根据类中的方法结构来定义,或者说在引用类中应当根据委托型对象的结构来生成响应事件的方法结构,比如两者都有哪些类型的参数、返回值的类型,也就是说两者要保持一致。同时,要正确地使用C#中的委托,就必须保持三个步骤:声明——实例化——调用。

  在上面的代码中,EventClass 类就体现了这个原则:
  1. 声明委托类型的对象: public delegate void CustomEventHandler(object sender, EventArgs e);
  2. 创建CustomEventHandler对象的实例CustomEvent:public event CustomEventHandler CustomEvent;
  3. 在InvokeEvent()方法中实现了对该事件的调用,引用事件。


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.