Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax :
public delegate void EventHandler(Object sender,EventArgs e)
sender
Type: System.Object
The source of the event.
e
Type: System.EventArgs
An EventArgs that contains event data.
************************************************************
Check the Code below :
private void btnShowDel_Click(object sender, EventArgs e)
{
int x = 6;
int y = 7;
int z = x + y;
MessageBox.Show("" + z);
MessageBox.Show(e.ToString());
}
On this above code, btnShowDel_Click is the eventhandler. Which is match with the predefined delegate System.EventHandler.
See the following code :
this.btnShowDel.Click += new System.EventHandler(this.btnShowDel_Click);
This code tells that, btnShowDel is an object and Click is the event. And btnShowDel raises the event. 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
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.
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;
**************************************************************
No comments:
Post a Comment