#189 – Adding an Event Handler to a Control Using Visual Studio

An event handler is code that executes in response to a user-initiated event–for example, code that executes when a user clicks a button.

In Visual Studio, you can add a new event handler in several ways.  You could edit the XAML directly and enter the name of a handler, or you can just double-click in the Properties window.

To add a new event handler from the properties window, start by left-clicking to select the control for which you want to add an event (e.g. a Button).

Next, find the desired event on the Events tab in the Properties window (e.g. Click).

Double-click in the empty area to the right of the event name, generating a name for the new handler (e.g. button1_Click)..

..and you’ll be taken to the code editor, where the body of the new event handler is located.

The name of the handler also now appears in XAML.

Advertisement