How to Code and Test a Windows Forms Application - How an application responds to events
(Page 3 of 5 )
Windows Forms applications are
event-driven. That means they work by responding to the events that occur on objects. To respond to an event, you code a special type of method known as an
event handler. When you do that, Visual Studio generates a statement that connects, or wires, the event handler to the event. This is called
event wiring, and it’s illustrated in figure 3-3.
In this figure, the user clicks the Exit button on the Invoice Total form. Then, Visual Studio uses the statement it generated to wire the event to determine what event handler to execute in response to the event. In this case, the btnExit.Click event is wired to the method named btnExit_Click, so this method is executed. As you can see, this event handler contains a single statement that uses the Close method to close the form.
This figure also lists some common events for controls and forms. One control event you’ll respond to frequently is the Click event. This event occurs when the user clicks an object with the mouse. Similarly, the DoubleClick event occurs when the user double-clicks an object.
Although the Click and DoubleClick events are started by user actions, that’s not always the case. For instance, the Enter and Leave events typically occur when the user moves the focus to or from a control, but they can also occur when code moves the focus to or from a control. Similarly, the Load event of a form occurs when a form is loaded into memory. For the first form of an application, this typically happens when the user starts the application. And the Closed event occurs when a form is closed. For the Invoice Total form presented in this figure, this happens when the user selects the Exit button or the Close button in the upper right corner of the form.
In addition to the events shown here, most objects have many more events that the application can respond to. For example, events occur when the user positions the mouse over an object or when the user presses or releases a key. However, you don’t typically respond to those events.
Event: The user clicks the Exit button

Figure 3-3. How an application responds to events
Wiring: The application determines what method to execute
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
Response: The method for the Click event of the Exit button is executed
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
Common control events
| Event | Occurs when… |
| Click | …the user clicks the control. |
| DoubleClick | …the user double-clicks the control. |
| Enter | …the focus is moved to the control. |
| Leave | …the focus is moved from the control. |
Common form events
Event | Occurs when...OOOccurs when… |
Load | …the form is loaded into memory. |
Closing | …the form is closing. |
Closed | …the form is closed. |
Concepts
- Windows Forms applications work by responding to events that occur on objects.
- To indicate how an application should respond to an event, you code an event handler, which is a special type of method that handles the event.
- To connect the event handler to the event, Visual Studio automatically generates a statement that wires the event to the event handler. This is known as event wiring.
- An event can be an action that’s initiated by the user like the Click event, or it can be an action initiated by program code like the Closed event.
How to add code to a form
Now that you understand some of the concepts behind object-oriented coding, you're ready to learn how to add code to a form. Because you'll learn the essentials of the C# language in the chapters that follow, though, I won't focus on the coding details right now. Instead, I'll focus on the concepts and mechanics of adding the code to a form.
Next: How to create an event handler for the default event of a form or control >>
More C# Articles
More By Murach Publishing
|
This article is excerpted from chapter three of the book Murach's C# 2005, written by Joel Murach (Murach, 2005; ISBN: 9781890774370). Check it out today at your favorite bookstore. Buy this book now.
|
|