Designing a User Control

A user control is technically the same as any other class that you author as a developer. Because a user control has a visual aspect to it, Visual Studio provides a designer, just as with Windows Forms, to help in the drag-and-drop creation of the control.

User controls are composite controls; that is, they are constructed from one or more existing .NET controls. As with a derived control, their user interfaces inherit from the native controls they are composed of, making them simple to build and use in the designer.

There are two approaches to the user control creation process: you can create a separate Windows Control Library project, or you can simply add a user control class to an existing Windows Forms project.

Creating a separate project enables the user control to live in its own assembly. If it is a separate assembly, you can treat the user control as the quintessential black box, giving you greater flexibility from a source control perspective and enabling you to share the control among multiple projects. For production scenarios, this is clearly the best route. However, for simple prototyping work, as you are doing here with the CSR form application, the ease and simplicity of just adding a new class to the existing project make this approach preferable to using the separate project approach. The class lives inside the same namespace as the form class.

If you were ever in a position to transition from prototyping to actual production development, nothing would preclude you from refactoring the control by simply copying the user control class file and embedding it in a separate control library project.

As soon as you add the user control class to the project, you are presented with the User Control Designer. The designer works the same way as the Windows Forms Designer; to build the user control, you drag components or controls from the Toolbox onto its surface.

Adding Controls

Obviously, the controls that you use to build your composite control entirely depend on how you envision the control’s functionality. As an example, to create an order display control, you need to think about the underlying data structure of an order. An order record might contain the following:

Image An order number

Image A series of dates that capture the date the order was placed, the date the order was shipped, and so on

Image A list of items included on the order

Image The billing information and shipping address

Image Miscellaneous comments

Because this is a lot of information to try to cram onto one screen, you can turn to the TabControl. A tab control is another general-purpose container control that enables you to organize content across several pages that are accessed via tabs. Within each tab, you can leverage the TableLayoutPanel and implement most of the order fields with simple label and text box pairs.

As mentioned earlier, the whole process of getting these controls into the user control works identically to the Windows Forms Designer: You drag and drop the controls from the Toolbox onto the user control design surface. Figure 20.31 shows the OrderDisplay user control with its user interface completed.

Image

FIGURE 20.31 Designing a user control.

Embedding the User Control

Now that you have a completed design for your user control, the only remaining step is to embed the control into your primary form. If you compile the project, Visual Studio automatically recognizes the user control class and includes an entry for the control in the Toolbox. From there, you are just a drag and drop away from implementing the OrderDisplay control.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset