Chapter 4. Wiring Up Events in the User Interface

In This Chapter

  • Understanding events and event handlers

  • Using Visual Studio to discover events

  • Creating event handlers with Visual Studio

  • Getting a grip on Windows and Web applications life cycles

Code fires in response to events, such as button clicks. Taking advantage of the events in an application and control life cycles allows you to run code at a precise moment in an application's or a control's life cycle. Instead of waiting for a user to click a button, you can run code as soon as your application starts — or even when a user's cursor leaves a text box.

In Visual Studio, responding to events is as simple as a double-click, in many cases. Visual Studio generates codes behind the scenes that wire up your code to the event that fires it.

Handling Events

In most modern applications, an application responds to actions that a user takes. Your application sits in a waiting state until a user comes along and clicks a button, a menu, or a drop-down list. When a user clicks a button, an action (the event) occurs. The code that executes when an event fires is an event handler.

Procedures that run when an event occurs — event handlers — um, handle events. Still, they're just procedures, just like any procedure you write. You're responsible for writing event handlers to handle any events to which you wish to respond.

For example, say that you want a message box to pop up when a user clicks OK on your Windows Form. The following code sample uses ...

Get Visual Studio® 2010 All-in-One For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.