DekGenius.com
[ Team LiB ] Previous Section Next Section

2.1 Windows Forms and the Control Class

Each different type of user interface element is represented by a specialized class deriving from Control. For example, top-level windows are represented by the Form class; each of the standard Windows control types has a corresponding class (such as Button and TreeView); you can also define custom controls by creating your own classes. All these inherit (either directly or indirectly) from the Control class.

Because all visual elements derive from Control, they share a single implementation of the features common to all controls. This ensures a certain minimum level of functionality and guarantees consistent behavior across all control types. The Control class defines standard properties, events, and methods for all the common features of user interface components. These include size and position, input handling, and appearance.

The Control class also defines the nature of the relationships controls on a form have with one another. As with classic Windows programming, a parent-child relationship is supported—a control may contain several child controls, and most controls (except top-level windows) have a parent control. The containment relationship is detailed in the next chapter, but it affects all controls for certain operations, such as moving and resizing windows. It also has some slightly more subtle implications for features such as focus management and control validation.

The Windows Forms framework defines a class hierarchy for the various kinds of controls. The Control class sits at the root of this hierarchy, but there are specializations for various types of controls, as Figure 2-1 shows. All the built-in controls (buttons, labels, tree views, etc.) inherit directly from the Control class. As you will see in Chapter 5, you can write your own controls that do the same.

Figure 2-1. The Control class hierarchy
figs/winf_0201.gif

In practice, most user-defined controls only inherit indirectly from the Control class. The single most common type of custom user interface element you will define will be top-level windows deriving from the Form class. The Form class is discussed in detail in the next chapter, as are its base classes, ScrollableControl and ContainerControl. The closely related UserControl class will be discussed in Chapter 5. For now, the main thing to be aware of is that a typical simple Windows Forms application will define a class derived from Form for each type of window it displays, and this class will be implemented using several of the built-in control types. We will now see how to use the facilities provided by Control that are common to all control types.

    [ Team LiB ] Previous Section Next Section