Ch07_Inside Controls.pdf

(128 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Chapter 7
Inside Controls
In this chapter, we discuss controls, the objects of the GUI, and then use five of them to
develop the first version of our sample application. [Comment 7_cs.1]
Parent / Child Properties ................................................................................................................ 20
Positional Properties ...................................................................................................................... 21
Data Binding Properties ................................................................................................................. 22
Control State Properties................................................................................................................. 22
Summary ........................................................................................................................................ 23
Core Methods.............................................................................................................................................. 23
Thread-Safe Function Calls ........................................................................................................... 23
Object Clean Up............................................................................................................................. 23
Coordinate Conversion .................................................................................................................. 24
Z-Ordering ...................................................................................................................................... 24
Visibility .......................................................................................................................................... 25
Summary ........................................................................................................................................ 25
Working with Control Type Information....................................................................................................... 25
Type Casting Controls.................................................................................................................... 26
Comparing Two Control References.............................................................................................. 26
Checking a Control's Type ............................................................................................................. 26
Summary ........................................................................................................................................ 27
The Five Controls of this Chapter ............................................................................................................... 27
Chapter 7 – Inside Controls
Page 1
Copyright © 2003 Paul Yao & David Durant
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
The TimeTracker Sample Application......................................................................................................... 31
Writing the Code ............................................................................................................................ 31
The Application Interface ............................................................................................................... 34
Coding Event Handlers .................................................................................................................. 36
Labels.......................................................................................................................................................... 38
Display – Yes; Update - No............................................................................................................ 39
Transparency ................................................................................................................................. 39
TextBoxes ................................................................................................................................................... 43
Display – Yes; Input – Yes ............................................................................................................. 43
ListBoxes / ComboBoxes ............................................................................................................................ 44
The List in ListBoxes ...................................................................................................................... 45
Using the ComboBox in the Sample Application ........................................................................... 47
Buttons ........................................................................................................................................................ 48
To Trigger Actions.......................................................................................................................... 49
Adding a control at run time ........................................................................................................... 49
Handling the “New” button Click event...........................................................................................50
Handling the “Add” button Click event. ..........................................................................................55
Handling the “Cancel” button Click event....................................................................................... 55
RadioButtons and CheckBoxes .................................................................................................................. 57
The RegisterMembers Sample Program ....................................................................................... 57
Radio Buttons................................................................................................................................. 58
Building RegisterMembers ............................................................................................................. 59
The Click Event .............................................................................................................................. 61
Check Boxes .................................................................................................................................. 61
Improving RegisterMembers .......................................................................................................... 61
Conclusion .................................................................................................................................................. 66
What are Controls?
What is a Compact Framework control? In a nutshell it is three things: It is an object, it has
a visible interface, and it was derived by Microsoft from a control that exists in the desktop
framework. The three characteristics; implemented as objects, visible to the user, descended
from the desktop framework, have determined what the Compact Framework controls are and
what they can do for us. [Comment 7_cs.2]
Visible Objects
Because they are objects they provide a valuable service in a nice neat package. And
because they are objects they expose their functionality through properties, methods and
events, PMEs as the Microsoft developers refer to them and as we will refer to them throughout
the book. Understanding the properties, methods and events that are available for a control,
and the interrelationship between them, is the key to successful Compact Framework
programming. [Comment 7_cs.3]
All Control classes derive – either directly or indirectly – from one of two base classes:
Component or Control . Like other objects, controls have code (the class methods) and data
(the class properties). A control owner manipulates a control by calling the class methods, and
further manages it by reading and writing the control's properties. [Comment 7_cs.4]
Consider the TextBox control. Among its other properties, the TextBox control has a
Multiline property – a boolean to select single or multi-line operation – and a Text property
– to access the text being displayed and edited. Among its various methods, some modify the
Chapter 7 – Inside Controls
Page 2
Copyright © 2003 Paul Yao & David Durant
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
visible state – Show() and Hide() – and others modify the Z-ordering – BringToFront() and
SendToBack() . Properties and methods provide two ways to connect to and manipulate a
control, but they are not the only means available. Events – and the ability to respond to events
– represent a very important part of the work of controls. [Comment 7_cs.5]
One way that controls differ from most other types of objects is that they are more likely to
raise events. Timer controls, for example, issue a Tick event when the desired time span has
passed. A TextBox control sends a TextChanged event when its contents change. Button
controls send Click events when tapped. The fact that controls are visible and are the provider
of the GUI means that they are constantly receiving taps and clicks and key strokes from the
user, events that the control must pass on to the application. Events, and their handlers, are a
central theme of this chapter. [Comment 7_cs.6]
The events raised by the control can either be ignored or processed by the application. You
write the code, called handlers , to process each event that you are interested in. You tie the
code to the event by adding a delegate, the .NET equivalent of a function pointer, to the event’s
list of handlers, as shown below. This code specifies that the function named
txtTaskComments _ TextChanged is to be called whenever the txtTaskComment’s
TextChanged event is raised. [Comment 7_cs.7]
this .txtTaskComments.TextChanged +=
new System.EventHandler( this .txtTaskComments_TextChanged);
Because controls are objects, they can be extended and they can be inherited from,
allowing us to expand the capabilities of the existing controls or to write our own new control
classes. These two capabilities are the subject of subsequent chapters and are not discussed
in this chapter. In this chapter we focus instead on the controls that the Compact Framework
Descended from the Desktop Framework
Because controls have descended from the desktop framework and have, by necessity, lost
some capability in that descent, they are both familiar to programmers with .NET Framework
experience and occasionally alien to these same programmers. This evolutionary history also
serves as a good starting point for our chapter, for an understanding of how Compact
Framework controls came to be helps our understanding of what they can do for us. [Comment
Microsoft created the Compact Framework controls by porting the desktop controls as
implemented in the .NET Framework – what we call the Desktop Framework – with the following
five design goals in mind. We discuss these design goals more fully in chapter one, and mention
them here to help put the subject of Compact Framework controls in perspective. [Comment
Build on the Benefits of the .NET Framework
Maintain consistency with the desktop
Run well on mobile and embedded devices
Expose the richness of target platforms
Preserve the look & feel of platforms
Maintain consistency with the desktop
The Compact Framework controls are implemented in the same namespace as the Desktop
Framework control, System.Windows.Forms . This namespace contain a strict subset of the
classes and controls found on its desktop counterpart. For example, the desktop framework
Chapter 7 – Inside Controls
Page 3
Copyright © 2003 Paul Yao & David Durant
870585274.005.png 870585274.006.png 870585274.007.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
supports thirty-five controls; the thirty controls in the Compact Framework
System.Windows.Forms namespace are a subset of these. A list of all supported Compact
Framework controls are shown in figure 7-1. [Comment 7_cs.11]
Figure 7-1 – .NET Controls Classes Supported in the Compact Framework v. 1.0
Control
Control Category
Covered in Chapter
Button
3 – Single-Item
7 – Inside Controls
CheckBox
3 – Single-Item
9 – Inside More Controls
ComboBox
4 – Multi-Item
ContextMenu
5 – Command Input
12 – Command Input Controls
Control
1 – Ultimate Parent
11- Custom Controls
4 – Multi-Item
8 – Data Binding
DataGrid
DomainUpDown
4 - Multi-Item
9 – Inside More Controls
Form
1 – Ultimate Parent
5 – Creating Forms
ImageList
7 - Background
12 – Command Input Controls
Label
3 – Single-Item
7 – Inside Controls
ListBox
4 – Multi-Item
7 – Inside Controls
4 – Multi-Item
9 – Inside More Controls
ListView
MainMenu
5 – Command Input
12 – Command Input Controls
MenuItem
5 – Command Input
12 – Command Input Controls
6 – Visual Numeric Value
10 – Numeric Controls
NumericUpDown
Panel
2 – Container Control
7 – Inside Controls
PictureBox
3 – Single-Item
9 – Inside More Controls
6 – Visual Numeric Value
10 – Numeric Controls
ProgressBar
RadioButton
3 – Single-Item
9 – Inside More Controls
HScrollBar
6 – Visual Numeric Value
10 – Numeric Controls
6 – Visual Numeric Value
10 – Numeric Controls
VScrollBar
StatusBar
3 - Single item
9 – Inside More Controls
TabControl
2 – Container Control
14 – Tab Controls
2 – Container Control
14 – Tab Controls
TabPage
TextBox
3 – Single-Item
7 – Inside Controls
Timer
7 - Background
15 – Timers, Notifications, and
Threads
ToolBar
5 – Command Input
12 – Command Input Controls
ToolBarButton
5 – Command Input
12 – Command Input Controls
6 – Visual Numeric Value
10 – Numeric Controls
TrackBar
TreeView
4 – Multi-Item
9 – Inside More Controls
And just as the Compact Framework controls are a subset of their desktop counterpart, so
too are the properties, methods, and events supported by Compact Framework controls a strict
subset of those properties, methods, and events supported by the Desktop Framework. For
example, the number of “core” events - those supported by all controls – was pared down from
17 in the Desktop Framework controls to just 7 in the Compact Framework controls: Disposed,
Chapter 7 – Inside Controls
Page 4
Copyright © 2003 Paul Yao & David Durant
870585274.001.png 870585274.002.png 870585274.003.png 870585274.004.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
ParentChanged, Validated, Validating, EnableChanged, GotFocus and LostFocus .
Run well on mobile and embedded devices
One of the oldest and most sacrosanct goals of Windows CE is to be small. The 25+
megabytes of the Desktop Framework would not work well on devices that might only have 32
megabytes, and so the Compact Framework team set out with a goal of occupying less than a
megabyte. They managed to get the system to occupy less than 2 megabytes – an important
accomplishment which makes the Compact Framework a good Windows CE citizen. On its way
to achieving its size goal, the Compact Framework team left out desktop-centric features such
as drag-and-drop, and large controls like the RichTextBox . Also, in working towards the goal,
the Compact Framework team took a page from the playbook of the Windows CE team.
Namely, if there are two or more ways to do the same operation, all are removed but one.
Hence, there are significantly fewer overloaded method calls in the Compact Framework than in
the Desktop Framework. [Comment 7_cs.13]
To run well also means having good performance – not appearing sluggish to the user, nor
taking too long to do common operations. To meet the performance goals, the Compact
Framework was benchmarked and tuned so that controls rely heavily on their Win32
counterparts. The Win32 controls reside in unmanaged code, and Compact Framework code
runs as managed code. There is a cost in crossing the boundary between managed and
unmanaged code. The Compact Framework team found that they could enhance the
performance of the controls by allowing only a carefully controlled subset of Win32 messages to
percolate as .NET events. [Comment 7_cs.14]
If you are an experienced Windows programmer – whether with the Desktop Framework,
Win32, MFC, or VB – you will find that the Compact Framework controls support a significantly
reduced set of events (or messages) from what you are used to working with. To give you an
idea of how significant, consider the System.Windows.Forms.TextBox control – a.k.a.
" TextBox " – which wraps the Win32 EDIT control. On the desktop .NET Framework, 66 events
are supported. In the Compact Framework, only 11 events are supported. As an example of
how drastic this change is, no control supports the Paint event. This means that you cannot
draw your own control by simply overriding the OnPaint method like you can in the desktop
Framework. This is not, incidentally, due to any reduction in support for messages in the
underlying Win32 controls compared to what those same controls do on desktop versions of
windows – rarely is that a factor. Instead, it is entirely to enhance the performance of the user-
interface that the number of available events has been reduced. [Comment 7_cs.15]
Expose the richness of target platforms
For almost every control, the Compact Framework relies on a native control to do most of
the work. (The one exception is the DataGrid control, which has been implemented entirely in
managed code.) This has obvious benefits in size and performance. It also provides a more
authentic look and feel for Compact Framework applications on each platform. [Comment 7_cs.16]
For example, the Compact Framework MainMenu class provides the basic support for
application menus. In a Pocket PC application, this menu appears – like in any other Pocket PC
application – at the bottom of a window. But on other Windows CE .NET devices, that same
Compact Framework program displays its main menu at the top of the window like a standard
Windows CE .NET application. While the underlying Win32 menu implementations on each
platform are different, the Compact Framework hides this from the developer in its MainMenu
and MenuItem classes. [Comment 7_cs.17]
Chapter 7 – Inside Controls
Page 5
Copyright © 2003 Paul Yao & David Durant
Zgłoś jeśli naruszono regulamin