Delegate, MulticastDelegate, EventHandler, EventHandlerList, EventHandlerList.ListEntry, Control, Component
Control的Events属性是由System.ComponentModel.Component 继承而来,它是EventHandlerList的实例.
EventHandlerList类有三个重要的方法:
public void AddHandler(object key, Delegate value);
public void RemoveHandler(object key, Delegate value);
private ListEntry Find(object key);
public void RemoveHandler(object key, Delegate value);
private ListEntry Find(object key);
示例:我们要为当前控件添加一个点击事件(Click).
第一步:创建一个静态只读的属性
public static readonly object ClickEvent=new object(); //它是以后事件查找委托的键(key),
第二步:注册事件
public event EventHandler Click
{
add
{
base.Events.AddHandler(ClickEvent,value);
}
remove
{
base.Events.RemoveHandler(ClickEvent,value);
}
}
----
以下是Events的定义:
private EventHandlerList events;
protected EventHandlerList Events
{
get
{
if (this.events == null)
{
this.events = new EventHandlerList();
}
return this.events;
}
}
protected EventHandlerList Events
{
get
{
if (this.events == null)
{
this.events = new EventHandlerList();
}
return this.events;
}
}