Authoring Controls

Windows Forms programming is based on controls. Controls allow you to encapsulate discrete units of functionality and provide a graphical representation of that functionality to the user. The .NET Framework provides a variety of tools and techniques to help you create your own custom controls for your applications.

In the .NET Framework, controls are specialized classes that include code for rendering a graphical interface. There are several possible sources for the controls you use in your applications. The controls included with Microsoft's Visual Studio .NET provide a broad range of functionality and allow development of client applications with a rich array of features.

You can also purchase controls from third-party developers and use them in your applications or use existing ActiveX controls in your applications. Third-party controls can provide functionality not found in the .NET Framework set of controls. If you need a control with a set of features not available in either the .NET Framework controls or the third-party controls, you can create your own controls.

Overview of Control Authoring

All controls in the .NET Framework inherit either directly or indirectly from the base class Control. This class provides all of the basic, low-level functionality that a control needs. For example, the Control class provides logic for handling user input through keyboard and mouse interaction; it provides logic for interacting with Windows; and it provides a set of properties, methods, and events that are common to all controls. It does not include any logic that creates the unique functionality of the control, nor does it contain any code for graphically rendering the control.

When creating a control, there are three basic techniques you can use:
  1. You can inherit from an existing control.
  2. You can create a user control.
  3. You can create a custom control.
  4. Inheriting from Existing Controls
The simplest way to create a new control is to inherit from a preexisting control. When inheriting from an existing control, the new control contains all of the functionality represented by the base control, but can also serve as a base for the addition of new functionality.

In addition to retaining all of the functionality of the base control, an inherited control also inherits the visual representation of the base control. Thus, a control that extends Button will look just like a regular Button. You do not need to create additional code to render the new control, but you can implement code to alter its appearance if you want.

Most Windows Forms controls can be used as a base class for inheritance unless they are specified as NotInheritable (sealed). For example, you could create a TextBox control with built-in validation or a PictureBox that allows the user to select filters to apply to the images it contains. You can also use inheritance to create controls that have the same functionality as the base control but have a different appearance. An example of this might be a Button that is round instead of square.

Inheriting from an existing control is the easiest and least time-consuming way to develop a new control. You should choose this technique if you need to replicate most or all of the functionality of an existing Windows Forms control but want to add custom functionality as well. You should also use this technique if you want to retain the functionality of an existing Windows Forms control but want to provide a new look and feel. You should not inherit from an existing Windows control if you need radically different functionality.

Inheriting from UserControl

Sometimes, a single control does not contain all of the functionality you need. For instance, you might want a control that you can bind to a data source to display a first name, last name, and phone number, each in a separate TextBox. Although it is possible to implement this logic on the form itself, it might be more efficient to create a single control that contains multiple text boxes, especially if this configuration is needed in many different applications. Controls that contain multiple Windows Forms controls bound together as a single unit are called user controls.

You can create a user control (sometimes called a composite control) by inheriting from the UserControl class. The UserControl class provides a base of functionality that you can build on by adding properties, method, events, and other controls. The UserControl designer allows you to add other Windows Forms controls to the design surface and create code to implement custom functionality. The result is a user-designed control made up of multiple Windows Forms controls that act as a single coherent unit.

Some limited design of the user control visual interface is possible, but most of the visual modifications you can make consist of simply configuring and positioning the constituent controls.

Creating a user control can be a relatively simple task or a complex one, depending on the level of complexity the control must expose. You should consider creating a user control if you need to combine the functionality of multiple existing controls and add custom logic to the unit.

Inheriting from Control

If you require a richer visual interface or functionality that is not achievable with a user control or an inherited control, you can create a custom control. Custom controls derive from Control, the base class for all controls. The Control class provides a lot of the functionality common to all controls, such as interacting with the mouse, and common events, such as Click. The control class also provides a set of properties that are useful for controls, such as Font, ForeColor, BackColor, Visible, and so on.

The Control class does not, however, provide any specific control functionality. All control logic specific to the control functionality must be programmed by the developer. Additionally, although the Control class provides many properties that support a visual depiction, the developer must program all of the code for creating the visual representation of the control.

Creating a custom control is the most time-consuming approach to designing your control. Not only must you code all of the specific functionality for your control, but you must also write code to render the control, which can be time-consuming. You should consider creating a custom control when you need a richness of form or functionality that cannot be supplied by an inherited control or a user control.

Adding Members to Controls

You can add properties, methods, fields, and events to your controls in the same manner in which you would add members to a class. You can create member declarations with any appropriate access level, and the form that hosts the control will be able to call any public members of that control.

Public properties that are added to controls are automatically displayed in the Properties window at design time. Thus, they can be edited and configured by any user who is using your application. If you do not want a property to be displayed in the Properties window, you must mark the property with the Browsable attribute and specify false.

Creating a New Appearance for an Existing Control

Another reason you might create an inherited control is to alter the appearance of an existing Windows Forms class. You can create a new look and feel for an existing control by overriding the OnPaint method and providing your own rendering code. If you want to change the shape of the control, you must set the control's Region property in the OnPaint method. A Region is a class that describes a regular or irregular region of the screen—similar to a GraphicsPath.

A Region can be created from a GraphicsPath. You can thus create a GraphicsPath to represent the shape of your control, and then assign the control to a new Region based on the GraphicsPath.

To create a user control

Create a class that derives from the UserControl base class.
Add Windows Forms controls to the UserControl designer and configure them appropriately.
Expose any constituent control properties.

Write any custom functionality for your user control.

Creating a Custom Control

Custom controls provide the highest level of configurability and customization. They are also the most time-consuming to develop. Because the Control class provides no base visual representation, you are obliged to write all of the code to render the visual representation of your control. In cases where a particular complex visual representation is desired, this can be the most time-intensive part of the control development process.

The process of rendering a control to the drawing surface is called painting. When the control receives instructions that it needs to be rendered, it raises the Paint event. This causes any handlers for the Paint event to be executed. In the Control class, the default handler for the Paint event is the OnPaint method.

The OnPaint method receives a single argument: an instance of PaintEventArgs. The PaintEventArgs class contains information about the drawing surface available to the control. Two notable members of PaintEventArgs are Graphics and ClipRectangle.

The Graphics member of PaintEventArgs is a Graphics object that represents the drawing surface of the control. This reference to a Graphics object is used to render the visual representation of the control. The ClipRectangle member is a rectangle that represents the area that is available for the control to draw in.

When the control is first drawn, the ClipRectangle represents the bounds of the entire control. At times, regions of the control might be covered by other controls and portions of the visual representation might be obscured. When the control is redrawn, the ClipRectangle only represents the region that needs to be redrawn. Thus, the ClipRectangle should not be used for determining the size of the control. To access the size of a custom control you should use the Size property.

By default, coordinates are measured relative to the upper left corner of the control, which is assumed to be point (0,0). Coordinate points are measured in pixels by default.

To create a custom control

Create a class that derives from the Control base class.

Add code to render the control in the OnPaint method.

Add any custom functionality to your control.

RELATED POST

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS


No comments:

Post a Comment