15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

summary ❘ 1147<br />

Creating your own user Controls<br />

In addition to using the built-in controls, it is also possible to create your own custom controls <strong>and</strong> then<br />

reuse these in your own applications. There are two types of custom controls — those derived from the<br />

UserControl base class <strong>and</strong> those derived from Control. A UserControl is the simplest type to create,<br />

as you can compose one from the other controls in the toolbox. Typically, you’ll write a custom control<br />

only when the built-in controls cannot be used to do what you want. Entire books have been written on the<br />

creation of custom controls, so here we’ll concentrate on user controls.<br />

In this section, you’ll build the user control shown in<br />

Figure 39-10, which includes an error provider validator,<br />

<strong>and</strong> exposes its fields for reuse. The code for this<br />

example is available in the 02 UserControl folder of<br />

the chapter downloads.<br />

To begin you need to construct a new user control,<br />

which can be done from the Solution Explorer or the<br />

project menu. Once it is created, you’ll have a class that<br />

derives from UserControl <strong>and</strong> can then drag <strong>and</strong> drop<br />

other controls onto the design surface. In this example<br />

you use a set of label controls <strong>and</strong> a set of text boxes to<br />

replicate the functionality shown in Figure 39-10.<br />

Once the UI has been laid out, you want to define anchor behavior for the controls. This is important to do<br />

as it permits the user control to resize properly when its size is changed. In the example control, each of the<br />

text boxes has had its alignment set to Left + Top + Right, <strong>and</strong> a small space on the right of the list boxes<br />

has been provided, which will show any validation errors.<br />

In addition to the text boxes, an ErrorProvider object has been added to the control so that validation<br />

errors can be shown while the user tabs around the control. The first two address fields <strong>and</strong> the postcode<br />

have validation errors assigned, so they are m<strong>and</strong>atory.<br />

Once you are happy with the design <strong>and</strong> implementation of your control, you can simply compile it, <strong>and</strong> it<br />

will be displayed within the tool palette in Visual Studio. You can then reuse that control in any application<br />

that needs it.<br />

summary<br />

figure 39-10<br />

This chapter has given you the basics for building Windows-based client applications. It explained many of<br />

the basic controls by discussing the hierarchy of the Windows.Forms namespace <strong>and</strong> examining the various<br />

properties <strong>and</strong> methods of the controls.<br />

The chapter also showed you how to create a basic user control. The power <strong>and</strong> flexibility of creating<br />

your own controls cannot be emphasized enough. By creating your own toolbox of custom controls,<br />

Windows-based client applications will become easier to develop <strong>and</strong> to test because you will be reusing<br />

the same tested components over <strong>and</strong> over again.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!