There are three basic techniques for the creation of custom Windows Forms controls in .NET, corresponding to three different starting points. This range of options offers the flexibility to choose a technique that allows an appropriate balance between simplicity and flexibility:
You can inherit from an existing control.
You can build a composite control (using the UserControl class as your starting point).
You can write a control from scratch (using the very simple Control class as your starting point).
These are in rough order of complexity, from simplest to most complex. Let’s look at each one with a view to understanding the scenarios in which each one is useful.
The simplest technique starts with a complete Windows Forms control that is already developed. A new class is created that inherits the existing control. This new class has all the functionality of the base class from which it inherits, and the new logic can be added to create additional functionality in this new class or, indeed, to override functionality from the parent (when permitted).
Here are some typical examples where it might make sense to extend an existing Windows Forms control:
A text box used for entry of American-style dates
A self-loading list box, combo box, or data grid
A Combobox that had a mechanism to be reset to an unselected state
A NumericUpDown control that generates a special event when it reaches 80 percent of its maximum allowed value
Each of these scenarios starts with an existing control that simply needs some additional functionality. The more often such functionality is needed in your project, the more sense it makes to package it in a custom control. If a text box that needs special validation or editing will be used in only one place, it probably doesn’t make sense to create an inherited control. In that case, simply adding some logic in the form where the control is used to handle the control’s events and manipulating the control’s properties and methods are probably sufficient.
In some cases, a single existing control does not furnish the needed functionality, but a combination of two or more existing controls does. Here are some typical examples:
A set of buttons with related logic that are always used together (such as Save, Delete, and Cancel buttons on a file maintenance form)
A set of text boxes to hold a name, address, and phone number, with the combined information formatted and validated in a particular way
A set of option buttons with a single property exposed as the chosen option
As with inherited controls, composite controls are only appropriate for situations that require the same functionality in multiple places. If the functionality is only needed once, then simply placing the relevant controls on the form and including appropriate logic right in the form is usually better.
Composite controls are the closest relative to VB6 UserControls, and because of that they are sometimes referred to as UserControls. In fact, the base class used to create composite controls is the UserControl class in Windows Forms.
If a control needs to have special functionality not related to any existing control, then it can be written from scratch to draw its own visual interface and implement its own logic. This option requires more work, but it enables you to do just about anything that is possible within .NET and Windows Forms, including very sophisticated drawing of a user interface.
To write a control from scratch, it is necessary to inherit from the Control class, which provides basic functionality such as properties for colors and size. With this basic functionality already built in, your required development tasks include adding any specific properties and methods needed for this control, writing rendering logic that will paint the control to the screen, and handling mouse and keyboard input to the control.