C# Event Handling

Graphical user interfaces in .NET and Visual C# uses event handling mechanism to handle events that occur while the program is running. Events are behaviors or happenings that occur when the program is running. Event handling is the process of monitoring for certain events to occur, and then executing codes when a specific event happens. Windows forms use event handling to add functionality and respond with the user. Without event-handling, forms and user interfaces are pretty much useless. This tutorial assumes that you already learned the concepts of delegates and events.

Events are declared using a delegate as a type. Delegates hold references to methods. The following is an example of declaring a delegate and an event.

public delegate void SampleEventHandler(int);

public event SampleDelegate SampleEvent;

Based on the declaration of the delegate, the methods that it can accept must not return a value(void) and accepts a single intargument. We then used this delegate type to create our event.

We will now add event handling to the event. Event handlers are methods that match the delegate type of the event and the ones that will be executed when the event occurs. Event handlers are attached to an event. Attached event handlers are executed when the event occurs. You can attach multiple event handlers to the event and they will all run when the event occurs. To attach an event handler to an event, you first create it. When creating an event handler, be sure that it matches the signature of the delegate that the event uses. For example, considering the delegate created above which has a return type of void and an int parameter, our event handlers should also have a void return type, and an int parameter (please note that access specifier is not important).

public void ShowMessage(int number)
{
    MessageBox.Show("Hello World");
}

We can then attach the event using the += operator like this:

SampleEvent += new SampleEventHandler(ShowMessage);

To activate the event, we call it passing the required arguments the same way we call methods.

SampleEvent(3);

Event Handling in Windows Forms


To demonstrate using events on windows forms, create a new Windows Forms Application and name it EventHandling. Double click the form and Visual Studio will automatically create an event handler and attach it to the Load event of the form. The Load event has a delegate type of EventHandler. Most events of controls have a delegate type of System.EventHandler. The following is the definition of the EventHandler delegate.

public delegate void EventHandler(object sender, EventArgs e)

As you can see with the definition of the delegate, it has no return type and has two parameters, an object, and an EventArgs instance. The object sender represents the control that activates the event. We will demonstrate its use later. The second argument is an instance of the EventArgs class. This can be called as the event argument and they contain data about the event that happened. The EventArgsis actually a base class and contains no useful members. Certain events will have event arguments that is derived from EventArgs and contain useful properties that the event handler can use. You will notice that the created event handler by Visual Studio matches the signature of the EventHandler.

private void Form1_Load(object sender, EventArgs e)
{

}

If you want to create event handlers manually, then be sure to follow the signature of the delegate type of the event. Visual Studio proposes a naming convention for event handlers, as seen in the generated event handler. When naming event handlers, type the name of the control(specified by its Name property) followed by an underscore and then the name of the event. You can ignore this convention if the event handler will be used by multiple events as we will see later.

Communicating with the Source Control


You have seen that the EventHandler delegate has two parameters, and the first of them is an object which represents the control that sent the event. Since it is of type object, any control can be a source of the event because every control is derived from the objectbase class. Since the sender is converted to an object, we need to convert it back to the proper control to access its useful properties. To demonstrate its use, add a button to the form. Change the Text property of the button to “Hello World!”.

Double click the button to generate an event handler for its Click event. Like the Load event, the Click event also has a type of EventHandler. Use the following code for the event handler.

private void button1_Click(object sender, EventArgs e)
{
    Button source = (Button)sender;
    MessageBox.Show("The message inside the button is " + source.Text);
}

Run the program and click the button. A message box will be shown telling the text inside the button that sent the Click event.

The first line of the event handler converts the sender object to a Button using casting so we can access its Text property. We then call the Show method of the MessageBox class to show the value of the Text property of the button that sent the event.

Event Arguments

The second parameter which actually varies depending on the event, is an event argument. The most common is the EventArgs which is the base class of event arguments. It has no properties you can use in your event handler. To demonstrate an event that has a different event argument, we will use the MouseClick event of the button which is a better version of the Click event. Since it is not the default event, we can access it in the events section of the Properties Window. Be sure that the button is selected in the designer. Click the thunderbolt icon to show the events of the button. We need to remove the Click event handler first so it will not conflict with the MouseClick event. Find the Click event and delete the value next to it. Then find and double-click the MouseClick event. The MouseClick event has a different delegate type of MouseEventHandler. It has the same signature as the ordinary EventHandlerdelegate, but it has a different type for its event argument which is MouseEventArgs. The MouseEventArgs is derived from EventArgs. It contains useful properties such as which button(left or right) of the mouse is clicked, the number of clicked done, how much the mouse wheel was rotated, and the point location of the click relative to the source control. Use the code below for the MouseClick event handler.

private void button1_MouseClick(object sender, MouseEventArgs e)
{
    MessageBox.Show("You clicked at point (" + e.X + ", " + e.Y + ")");
}

The code above uses the event argument parameter to access the x and y coordinates of the point where the mouse was clicked. The output below my vary depending on where you click on the button.

There are many more event argument types and each offers useful properties about the event that took place.

Using the Properties Window to Attach Event Handlers

We already know that aside from double clicking controls, we can use the Properties Window to attach event handlers to events. We click the thunderbolt icon to go to the Events section of the Properties Window.

The top combo box is used to select controls in the Designer. This is useful when you can’t select certain controls because they aren’t visible or very small.

Find the desired event. To attach an event handler to an event you can double click the name of the event to generate an event handler for the specified event. Alternatively, if you already have created event handlers, you can click the dropdown button to view all the valid event handlers that match the signiture of the delegate type of the specified event.

Using Identical Event Handlers for Multiple Events

Please note that you can use a single event handler for multiple events that has the same delegate type. To do this, you need to first create the event handler with the proper signiture. Then go to the Events section of the Properties Window and find the events you want. Instead of double clicking, choose the event handler you created. Now find another event and choose the same event handler.