Jun 24, 2012

Custom Controls in .NET

Today, we will talk about Controls in .NET. As we all know, a user control is essentially a component with a visual representation. As such, it might consist of one or more Windows Forms controls, components, or blocks of code that can extend functionality by validating user input, modifying display properties, or performing other tasks required by the author. User controls can be placed on Windows Forms in the same manner as other controls.  However a custom controls never end that simple, so there are types of costom controls like this:

4 Types of Custom Controls in .NET

User controls are the simplest type of control. They inherit from the System.Windows.Forms.UserControl class, and follow a model of composition.
Usually, user controls combine more than one control in a logical unit (like a group of text boxes for entering address information).
Inherited controls are generally more powerful and flexible. With an inherited control, you choose the existing .NET control that is closest to what you want to provide. Then, you derive a custom class that overrides or adds properties and methods.
Owner-drawn controls generally use GDI+ drawing routines to generate their interfaces from scratch. Because of this, they tend to inherit from a base class like System.Windows.Forms.Control. Owner-drawn controls require the most work and provide the most customizable user interface.
Extender providers, which aren't necessarily controls at all. These components add features to other controls on a form, and provide a remarkable way to implement extensible user interface.

Reasons of Custom Controls in .NET

To create controls that abstract away unimportant details and are tailored for a specific type of data. You saw this model in Chapter 6 with custom ListView and TreeView examples.
To create controls that provide entirely new functionality, or just combine existing UI elements in a unique way.
To create controls with a distinct original look, or ones that mimic popular controls in professional applications (like Microsoft's Outlook bar) that
aren't available to the masses.
In .NET, creating a custom control is as easy as creating an ordinary class. You simply inherit from the best possible ancestor and add the specific features you need. Best of all, you can create a custom control class as part of an existing project, and then decide later to place it in a separate assembly that can be shared with other programmers.

Ways of adding user controls:
 User control can be added at design-time or run-time. Other controls is usually added at run-time.
 

No comments:

Post a Comment