An event is a message sent by an object to signal the occurrence of an action. The action could be caused by user interaction, such as a mouse click, or it could be triggered by some other program logic.
A delegate is a class that can hold a reference to a method that matches its signature. A delegate is thus equivalent to a type-safe function pointer or a callback.
Events are defined with the help of delegates.
Declaring and Implementing Events:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
//create custom delegate
public delegate void Mydelegate();
public class Eventdemo
{
//create custom event
public event Mydelegate MyCustomEvent;
//Method is defined to fire our custom event.
public void ShowMessage()
{
//fire our custom event
MyCustomEvent();
}
}
public Form1()
{
InitializeComponent();
}
//this method is called when the button is pressed, kept on form.
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Predefined click event of button");
//create object of Eventdemo class to subscribe for its events.
Eventdemo objEventDemo = new Eventdemo();
//subscribe to MycustomEvent declared in Eventdemo class using += syntax which automatically generates callback method[objEventDemo_MyCustomEvent]
objEventDemo.MyCustomEvent += new Mydelegate(objEventDemo_MyCustomEvent);
//call the method of Eventdemo class which will fire custom event.
objEventDemo.ShowMessage();
}
//this method is called when the "MyCustomEvent" Event is fired
void objEventDemo_MyCustomEvent()
{
//unsubscribing the events.
objEventDemo.MyCustomEvent -= new Mydelegate(objEventDemo_MyCustomEvent);
//provide your implementation here what you want to do when your custom event will fire.
MessageBox.Show("My Custom Event is Fired"); }
}
}
Notes:
1. Let’s look at the custom Event first. Below is the event declaration, which is done with the help of Mydelegate which we have created.
//create custom event
public event Mydelegate MyCustomEvent;
2. The += syntax is used to subscribe to an event:-
//subscribe to MycustomEvent declared in Eventdemo class using += syntax which automatically generates callback method[objEventDemo_MyCustomEvent]
objEventDemo.MyCustomEvent += new Mydelegate(objEventDemo_MyCustomEvent);
3.To unregister or to unsubscribe an event, use the -= with the same syntax.
//subscribe to MycustomEvent declared in Eventdemo class using += syntax which automatically generates callback method[objEventDemo_MyCustomEvent]
objEventDemo.MyCustomEvent -= new Mydelegate(objEventDemo_MyCustomEvent);
4. Firing an event looks just like a method call, as shown below:
//fire our custom event
MyCustomEvent();
This was how to implement Custom Events from scratch with the help of delegate. However, much of the Event programming you'll do with pre-defined Events and Delegates.
Here is the predefined event of a button control :-
Button1.Click += new EventHandler(button1_Click);
The Click Event already belongs to the Button class and all we have to do is to subscribe to its click event. Similarly, the EventHandler Delegate already exists in the System namespace of the .NET Frameworks Class Library. All you really need to do is define your callback method (delegate handler method) that is invoked when someone presses the Button1. The button1_Click () method, shown below, confirms to the signature of the EventHandler Delegate, which you can look up in the .NET Framework Class Library reference.
public void button1_Click (object sender, EventArgs ea)
{
MessageBox.Show("You Clicked My Button!");
}
{
MessageBox.Show("You Clicked My Button!");
}
Any time the Button1 button is pressed with a mouse, it will fire the Click Event, which will invoke the button1_Click () method. The Button1 class takes care of firing the Click Event and there's nothing more you have to do. Because it is so easy to use pre-defined Events and Delegates.
Summary
This completes this lesson, was an introduction to Delegates and Events. You learned how to declare and implement Events, which provide dynamic run-time method invocation services.