Export (0) Print
Expand All
Expand Minimize
1 out of 5 rated this helpful - Rate this topic

EventHandler Delegate

Represents the method that will handle an event that has no event data.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public delegate void EventHandler (
	Object sender,
	EventArgs e
)
/** @delegate */
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public delegate void EventHandler (
	Object sender, 
	EventArgs e
)
Not applicable.

Parameters

sender

The source of the event.

e

An EventArgs that contains no event data.

The event model in the .NET Framework is based on having an event delegate that connects an event with its handler. To raise an event, two elements are needed:

  • A delegate that identifies the method that provides the response to the event.

  • A class that holds the event data.

The delegate is a type that defines a signature, that is, the return value type and parameter list types for a method. You can use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.

The standard signature of an event handler delegate defines a method that does not return a value, whose first parameter is of type Object and refers to the instance that raises the event, and whose second parameter is derived from type EventArgs and holds the event data. If the event does not generate event data, the second parameter is simply an instance of EventArgs. Otherwise, the second parameter is a custom type derived from EventArgs and supplies any fields or properties needed to hold the event data.

EventHandler is a predefined delegate that specifically represents an event handler method for an event that does not generate data. If your event does generate data, you must supply your own custom event data type and either create a delegate where the type of the second parameter is your custom type, or use the generic EventHandler delegate class and substitute your custom type for the generic type parameter.

To associate the event with the method that will handle the event, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.

For more information about event handler delegates, see Events and Delegates (.NET Framework Guide).

The following code example demonstrates the declaration of an event handler delegate that does not use event data. The EventHandler class is the type of the event delegate, sender is the object that raises the event, and e is an event data object that contains no data. The second line of code in the example defines the event member in your class for an event that has no data.

public delegate void EventHandler(Object sender, EventArgs e);
public event EventHandler NoDataEventHandler;

public delegate void EventHandler(Object sender, EventArgs e);

public EventHandler noDataEventHandler = null;

/** @event 
 */
public void add_NoDataEventHandler(EventHandler p)
{
    noDataEventHandler =(EventHandler)System.Delegate.
        Combine(noDataEventHandler, p);
}//add_NoDataEventHandler

/** @event 
 */
public void remove_NoDataEventHandler(EventHandler p)
{
    noDataEventHandler =(EventHandler)System.Delegate.
        Remove(noDataEventHandler, p);
}// remove_NoDataEventHandler


Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.