-
One common use of a delegate is to register methods to be
executed when an event occurs.
-
The namespace System declares the delegate:
delegate void EventHandler(object sender, EventArgs e);
-
The class Button
has a field called Click:
public event EventHandler Click;
-
Suppose we declare an instance of the class
Button:
private Button iAddButton = new Button();
-
A method can be added to this Button's
Click field using:
iAddButton.Click += new EventHandler(iHandleClick);
-
Add the
keyword event
to the declaration of a delegate to restrict access to the
delegate.
-
This assumes the existence of the method:
protected void iHandleClick(object sender, EventArgs e)
{
...
}