Datasheet
It ’ s also possible to have third - party controls in your environment. Such controls can be registered with Visual
Studio and are then displayed within every project you work on. When controls are added to the Toolbox they
typically appear in their own custom categories so that they are grouped together and therefore easy to fi nd.
Return to the button you ’ ve dragged onto the form; it ’ s ready to go in all respects. However, Visual Studio has
no way of knowing how you want to customize it. Start by going to the Properties window and changing the
Text property to Run Code . You can then change the button ’ s ( Name ) property to ButtonTest . Having made
these changes, double - click the button in the display view. Double - clicking tells Visual Studio that you want
to add an event handler to this control, and by default Visual Studio adds an On _ Click event handler for
buttons. The IDE then shifts the display to the Code view so that you can customize this handler (Figure 1 - 24
shows the code for this event handler being edited).
FIGURE 1 - 24
Although the event handler can be added through the designer, it ’ s also possible to add event handlers from
Code view. After you double - click the button, Visual Studio will transfer you to Code view and display your
new event handler. Notice that in Code view there are drop - down lists on the top of the edit window. The
boxes indicate the current object on the left — in this case, your new button — and the current method on
the right — in this case, the click event handler. You can add new handlers for other events on your button
or form using these drop - down lists.
The drop - down list on the left side contains the objects for which event handlers can be added. The drop -
down list on the right side contains all the events for the selected object. For now, you have created a new
handler for your button ’ s click event, so let ’ s look at customizing the code associated with this event.
Customizing the Code
With the code window open to the newly added event handler for the ButtonTest control, you can start to
customize this handler. Note that adding a control and event handler involves elements of generated code.
Enhancing a Sample Application
❘
41
CH001.indd 41CH001.indd 41 4/5/10 11:56:54 AM4/5/10 11:56:54 AM