

























首先从controlInstance.Click事件开始. 用Reflector反编译System.Windows.Forms.Control类可以看到对Click事件的定义:
[System.Windows.Forms.SRCategory("CatAction"), System.Windows.Forms.SRDescription("ControlOnClickDescr")]
public event EventHandler Click
{
add
{
base.Events.AddHandler(Control.EventClick, value);
}
remove
{
base.Events.RemoveHandler(Control.EventClick, value);
}
}

这里的Control.EventClick是一个只读的静态私有属性,它是以后事件查找委托的键(key),请记住这个.
private static readonly object EventClick = new object();
private EventHandlerList events;
protected EventHandlerList Events
{
get
{
if (this.events == null)
{
this.events = new EventHandlerList();
}
return this.events;
}
}

public void AddHandler(object key, Delegate value)
{
EventHandlerList.ListEntry entry1 = this.Find(key);
if (entry1 != null)
{
entry1.handler = Delegate.Combine(entry1.handler, value);
}
else
{
this.head = new EventHandlerList.ListEntry(key, value, this.head);
}
}

button1.Click += new EventHandler(OnButtonClick);
protected void OnButtonClick(object sender, System.EventArgs e)
{
// 你的处理函数
}

// ==++==
//
//
// Copyright (c) 2002 Microsoft Corporation. All rights reserved.
//
// The use and distribution terms for this software are contained in the file
// named license.txt, which can be found in the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// You must not remove this notice, or any other, from this software.
//
//
// ==--==
namespace System {
using System;
/**//// <include file='doc\EventHandler.uex' path='docs/doc[@for="EventHandler"]/*' />
[Serializable()]
public delegate void EventHandler(Object sender, EventArgs e);}
protected override sealed Object DynamicInvokeImpl(Object[] args)
{
if (_prev != null)
_prev.DynamicInvokeImpl(args);
return base.DynamicInvokeImpl(args);
}

[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void OnClick(EventArgs e)
{
if (this.CanRaiseEvents)
{
EventHandler handler1 = (EventHandler) base.Events[Control.EventClick];
if (handler1 != null)
{
handler1(this, e);
}
}
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。