ch10_Building Custom Controls.pdf

(283 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Chapter 10
Building Custom Controls
In this chapter, we cover developing custom controls; including extending existing controls,
building composite controls, and creating controls that provide their own rendering. Coverage
includes developing for multi threaded applications, animation, and incorporating your custom
controls into the Windows Forms Designer toolbox in Visual Studio .NET. Key background
information is presented, design issues are discussed and recommended coding practices are
demonstrated [Comment 10cs.1] .
Custom control classes can either respond to events or override the base event handler
Design decision #1: Exposing Custom Control Content ................................................... 18
Design Decision #2: Where to validate / filter incoming data? ......................................... 19
The DateBox code ............................................................................................................ 21
Testing the DateBox.......................................................................................................... 29
The DateBox summary ..................................................................................................... 31
The Water objects example ........................................................................................................... 31
More design decisions ...................................................................................................... 32
The WaterTemp object ..................................................................................................... 34
The WaterBox object......................................................................................................... 38
The Water object classes summary.................................................................................. 43
Creating Container Controls........................................................................................................................ 43
The TaskStatus example .................................................................................................. 44
Container Controls Summary............................................................................................ 46
Creating New Controls ................................................................................................................................ 46
Design Tips .................................................................................................................................... 46
Custom controls support more events than built-in controls............................................. 46
Chapter 10 – Custom Controls
Page 1
Copyright © 2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
You can override the drawing only for controls directly derived from Control ............... 47
A redrawing can be requested by the runtime or initiated by the application. .................. 47
Drawing in a Custom Control ............................................................................................ 47
The enhanced TaskStatus control .................................................................................................48
TaskStatus summary ..................................................................................................................... 51
Adding Animation to a Custom Control....................................................................................................... 51
The Game of Life ........................................................................................................................... 51
Providing Consistent Speed and Minimal Flicker .......................................................................... 57
Object Reuse ................................................................................................................................. 61
Animation Summary....................................................................................................................... 62
Authoring Custom Controls for the Multi Threaded Environment ............................................................... 62
A Control’s Thread and Service Threads....................................................................................... 62
The Multi Thread Example ............................................................................................................. 65
Multi Threading summary ................................................................................................. 71
Adding a Control to Visual Studio .NET ...................................................................................................... 71
Obstacles to putting custom controls in the Visual Studio .NET Toolbox...................................... 71
A compact framework control cannot be added to the toolbox......................................... 71
You are developing a desktop control, but one that is different from a normal desktop
control. .............................................................................................................................. 71
You see build errors. ......................................................................................................... 71
Testing design-time controls takes a bit more effort. ........................................................ 71
Freeze version numbers during testing. ........................................................................... 72
One or two sets of source files?........................................................................................ 72
Developing a Design-Time Custom Control................................................................................... 72
Setting Up the Design-Time Project.................................................................................. 72
The Development Process................................................................................................ 73
Setting Up the Project ....................................................................................................... 73
Building and Installing the Design-Time DateBox Control ................................................ 79
Setting Default Property Values........................................................................................ 82
Design-time controls summary ......................................................................................... 83
Summary ..................................................................................................................................................... 83
The .NET Compact Framework provides you with a variety of features for creating
applications. Included among these features are the controls in the Toolbox. Sometimes the
functionality of the built-in controls is not quite enough and what you can get comes close to
being what you want, but doesn’t quite make it. In such cases, you can create a custom control
to fill the gap. Here are some examples of when you might want to create a custom control:
You want a TextBox, but one that accepts and displays only dates, and only dates that
lie within a user specified range. [Comment 10cs.4]
You have placed a set of controls on a form to display name and address information and
you would like to use this set of controls in several other applications. [Comment 10cs.5]
You need to accept mouse or keyboard input in a non-standard way, perhaps to use
mouse input to draw a picture, or the keyboard to navigate the display of some data.
You wish to display application-specific information, such as the current state of a game
or bar graph that represents the progress of a project. [Comment 10cs.7]
Compact Framework provides for all of the above by letting you extend the capability of
existing controls. Since all .NET controls, from the universal base class of Control to its most
complex derivation, are object classes, all of them can be extended through inheritance.
Chapter 10 – Custom Controls
Page 2
Copyright © 2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
An Opinion: Work versus Results
We find that inheritance provides a good trade-off between effort and results; that
is, the work required to add functionality seems to be directly proportional to the
amount of functionality. In contrast, environments like VB6 allowed you to add
functionality very easily until you bumped into a wall, at which point producing
additional functionality became a great effort as you had to climb over that wall.
In .NET the walls are fewer and lower, and the effort seems to us to be roughly
proportional to the amount of functionality produced. [Comment 10cs.9]
You can extend existing controls in one of three ways:
You can derive from an existing control. There is an existing object, such as a TextBox ,
that does most of what you need but not all of what you need. You wish to extend the
functionality of this existing object to handle your special case. A TextBox that accepts
and displays only dates would be example of this. [Comment 10cs.10]
You can create composite controls. The functionality that you want is not found in a
single control, but can be achieved by combining several existing controls. You wish
to package these controls together into a single custom control, such as a name and
address control, for use in one or more applications. Such controls are referred to as
composite controls. [Comment 10cs.11]
You can create new controls. You need a control that handles input in a unique way,
or you need a control which is graphically unique – that is, one that displays
information in a manner that is not provided by any existing Compact Framework
control, such as a control that displays the current state of a game . [Comment
This chapter provides examples of each approach to creating custom controls. Whatever
approach you take, all have this in common: a custom control is always defined as a new class.
As such, success in creating Compact Framework custom controls requires you to understand
the existing controls and the base classes on which they are built. This is particularly important if
you have experience building custom controls for the Desktop Framework because the
Compact Framework controls are different from their desktop counterparts. In some cases, as
we discuss in chapter 7, Inside Controls , the Compact Framework control is a slimmed down
version of what you find on the desktop; in other cases, such as the DataGrid control, the
Compact Framework control is quite different, having been written from scratch in managed
code. In general, Compact Framework controls are lighter weight versions of their desktop .NET
Framework counterparts. [Comment 10cs.13]
Packaging Custom Controls
You have two choices when packaging a custom control: inside a Compact
Framework program (.exe) file, or in a shared class (.dll) library. Assuming that
you have the original source code, you can package any custom control within
your program file by adding the source files to your program's Visual Studio .NET
One of the benefits of putting a custom control into a DLL is that the control can
be shared with others. In addition, you need to put a custom control into a DLL to
add the control to the Visual Studio .NET toolbox (details of which appear in this
Chapter 10 – Custom Controls
Page 3
Copyright © 2003 Paul Yao & David Durant
870585256.004.png 870585256.005.png
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Controls as Objects
A key design element of the .NET programming model is its object-oriented nature. Controls
– whether built-in or custom – are objects. Because objects are created from 1 classes, to create
a custom control you must define a new class. [Comment 10cs.16]
There is, however, more to creating a custom control than simply defining a new class. In
this section, we provide some design recommendations for custom control classes, and then
examine the core elements that custom controls typically provide: the properties, methods, and
events – that is, the PMEs. [Comment 10cs.17]
Design Recommendations
The path to success in creating a custom control involves understanding the basic elements
of the existing, built-in controls. These controls contain many examples of good practices, and
also show some patterns that programmers expect to find in controls (we assume that a custom
control created by one programmer is to be used – or, in the future, maintained – by another
programmer). [Comment 10cs.18]
Most controls are wrappers around native Window classes
As we mention in chapter 7, Inside Controls , most Compact Framework controls are wrappers
around controls which are part of the underlying Win32 layer of Windows CE. Most of the work
of the control is done in unmanaged code, and this factor contributes to the relatively small size
of the Compact Framework runtime. Many events (a.k.a. "messages") which are received and
processed by the Win32 control are not sent into managed code, which is why only a subset of
the defined events are available in managed code. This factor contributes to the snappy
performance of the Compact Framework user-interface. [Comment 10cs.19]
Since the Compact Framework relies on Win32 controls, you can as well. And so, one way
to create a custom control is to create a managed code wrapper around a Win32 control. This
chapter does not include an example of this recommendation, but because there has been a
substantial amount of Win32 code written for Windows CE, this technique is worth mentioning
as one possible implementation choice. [Comment 10cs.20]
Some controls – such as the DataGrid – are implemented entirely in managed code. And
so, another way to create a custom control is to implement it entirely in managed code. If you
choose to do that, one question you may ask yourself is this: what base class should I start
with? That is the topic of the next point. [Comment 10cs.21]
Selecting a base class
The first step in creating a custom control involves picking a base class. We see four
choices for a Compact Framework programmer. We list these choices below, from most to least
preferred – meaning from least to most work required to create a working control: [Comment
An Existing Control Class – The most common custom control is one that changes the
behavior of an existing control in a very small way. If you want a control that behaves
similar to a TextBox , inherit from the TextBox class and proceed. No point
reinventing the wheel. [Comment 10cs.23]
System.Windows.Forms.Control – Use this base class for custom controls that
stand alone on a form alongside other controls. In other words, this base class wraps
around a Win32 window which has a presence on the user's display, and which receives
1 Or "instantiated", in the lingo of object-oriented programming.
Chapter 10 – Custom Controls
Page 4
Copyright © 2003 Paul Yao & David Durant
870585256.006.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
mouse and keyboard events. [Comment 10cs.24]
System.ComponentModel.Component – Use this base class for controls which do
not wrap a Win32 window. In other words, use this as a base class for objects without a
user-interface, or which supplement the user-interface of another control. This is the base
class used by these Compact Framework control classes: Timer , Menu , ImageList ,
ToolBarButton , and ColumnHeader (used by the ListView control). [Comment
Object – The ultimate base class for all Compact Framework classes is Object . This
class could be used for a Compact Framework control, as an alternative to Component .
The advantage of Object is that it provides the lightest class and therefore the least
overhead. But such a class would not port well to the desktop .NET Framework, where
Component would be a better choice. [Comment 10cs.26]
The Foundation of all controls are PMEs
As we discuss in chapter 7, Inside Controls, the operation of all controls is defined by the
supported set of PMEs – properties, methods, and events. For this reason, new control classes
extend the functionality of existing control classes by adding new PMEs and overriding existing
For example, the code in listing 10-1 defines a custom text box that: [Comment 10cs.28]
Is twice as wide as the normal TextBox [Comment 10cs.29]
Stores its Text as upper case [Comment 10cs.30]
Provides a method to return the Text as lower case. [Comment 10cs.31]
We present an explanation of the code through out this chapter, it appears here as a
general illustration of adding and overriding properties, methods and events (PMEs). [Comment
Listing 10-1 A Custom TextBox that inherits from the built-in TextBox clas s [Comment
using System;
using System.Windows.Forms;
namespace YaoDurant.Gui
{
public class TextBoxPlus : TextBox
{
// Constructor
public TextBoxPlus()
{
this.Width *= 2;
}
// Overriden method
protected override void OnLostFocus(EventArgs e)
{
this.Text = this.Text;
base.OnLostFocus(e);
}
// Overriden property
Chapter 10 – Custom Controls
Page 5
Copyright © 2003 Paul Yao & David Durant
870585256.001.png 870585256.002.png 870585256.003.png
 
Zgłoś jeśli naruszono regulamin