Previous Page
Next Page

Understanding GUI Events

As mentioned earlier, the .NET Framework classes and controls used for building GUIs employ events extensively. You'll see and use GUI events on many occasions in the second half of this book. For example, the Button class derives from the Control class, inheriting a public event called Click of type EventHandler. Let's see this in code. The EventHandler delegate expects two parameters; a reference to the object that caused the event to be raised, and an EventArgs object that contains additional information about the event:

namespace System
{
    public delegate void EventHandler(object sender, EventArgs args) ;

    public class EventArgs
    {
        ...
    }
}

namespace System.Windows.Forms
{
    public class Control :
    {
        public event EventHandler Click;
        ...
    }

    public class Button : Control
    {
        ...
    }
}

The Button class automatically raises the Click event when you click the button on-screen (how this actually happens is beyond the scope of this book). This arrangement makes it easy to create a delegate for a chosen method and attach that delegate to the required event. The following example shows a Windows form that contains a button called okay, a method called okay_Click, and the code to connect the Click event in the okay button to the okay_Click method:

class Example : System.Windows.Forms.Form
{
    private System.Windows.Forms.Button okay;
    ...
    public Example()
    {
        this.okay = new System.Windows.Forms.Button();
        this.okay.Click += new System.EventHandler(this.okay_Click);
        ...
    }

    private void okay_Click(object sender, System.EventsArgs args)
    {
        // Your code to handle the Click event
    }
}

When you use the Designer View in Visual Studio 2005, the IDE generates the code that subscribes methods to events automatically. All you have to do is write the logic in the event handling method.

NOTE
It is possible to add a method to an event without creating an instance of a delegate. You could replace the following statement:
this.okay.Click  += new  System.EventHandler (this.okay_Click);
with this:
this.okay.Click += this.okay_Click;
However, the Windows Forms designer in Visual Studio 2005 always generates the first version.

The events that the GUI classes generate always follow the same pattern. The events are of a delegate type whose signature has a void return type and two arguments. The first argument is always the sender of the event and the second argument is always an EventArgs argument (or a class derived from EventArgs).

The sender argument allows you to reuse a single method for multiple events. The delegated method can examine the sender argument and respond accordingly. For example, you can use the same method to subscribe to the Click event fo two buttons (you add the same method to two different events). When the event is raised, the code in the method can examine the sender argument to ascertain which button was clicked.


Previous Page
Next Page