.NET Custom Controls: Designer Basics
In This Chapter
The ControlDesigner Base Class
The IconButton Designer
Adding a Toolbox Bitmap
A control designer defines the behavior and UI presentation of a control during design-time. In VS .NET, forms can be created using the Forms Designer. This Forms Designer allows controls, such as buttons, menus, and toolbars, to be drawn on the form rather than being created pragmatically. Each control has an associated designer that defines the behavior of the control during the visual design-time process of building a Windows Forms application.
During design-time, a control's properties can be manipulated using the Property Grid. The control should appear very similar to the runtime look while being designed, with a few exceptions. These exceptions include the handling of a control's Visible property and any designer clues such as the placement grid. The placement grid is the series of dots used for aligning controls during design-time that are not shown at runtime. In general, however, a control's appearance during design-time should give an accurate representation of the control's appearance during runtime.
Certainly, setting the control's Visible property to false during design-time should not make the control invisible. If this were the case, there would be no way to select the control and continue to visually design it.
Designers are an important part of the equation in developing custom controls. By providing a rich design-time experience, application developers can visually build applications using the custom control you've created. The UI presentation is not a designer's sole responsibility; designers must also provide for proper serialization of the code generated for the control, in order for the control to work properly at runtime. The code for the construction of the control, along with the necessary property settings, is serialized within the InitializeComponent method of the form hosting the control. This is the reason for the all-too-familiar comment on the method stating, "This code should not be modified as the designer will OVER WRITE this method when serializing the form's design state."
The ControlDesigner Base Class
Just as .NET provides base classes for developing controls, there also exists a set of base classes for implementing designers. In Chapter 5, "Advanced Control Development," the designer base classes are covered in more detail; for now, the ControlDesigner base class is the focus.
The ControlDesigner base class provides the bare-bones functionality for designing a control. Figure 3.1 shows a UML diagram of the ControlDesigner inheritance chain and supported interfaces. UML, or Unified Modeling Language, diagrams are helpful tools for visualizing the various components and classes of any software project.
Figure 3.1 The ControlDesigner hierarchy.
The base class for ControlDesigner is ComponentDesigner. The ComponentDesigner base class provides support for general component design and is not intended to be used directly for providing design-time support for controls. Its purpose is to provide common functionality for component design-time support.
The ControlDesigner base class serves as a starting point for creating control designers and implements the necessary interfaces for VS .NET. The ControlDesigner base class will serve as the base class for the IconButtonDesigner developed in this chapter. Remember that each control has an associated designer class. This association between the control and its designer class is created by specifying the designer class of a control through the use of an attribute. The DesignerAttribute is used for this purpose.