ch05_Creating Forms.pdf

(201 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 5
Creating Forms
In this chapter, we discuss forms, the basic element of a .NET Compact Framework GUI application,
examining their unique characteristics and their role in providing the user interface. [Comment 5cs.1]
The Weather Sample Application ..................................................................................................15
Tracking Forms .............................................................................................................................. 18
The Controls Collection.................................................................................................................. 20
Multi-threading ............................................................................................................................... 21
Inheritance and Visual Inheritance.............................................................................................................. 22
Conclusion .................................................................................................................................................. 28
What are Forms?
.NET Compact Framework applications present information and receive input by displaying
one or more forms to the user. These forms have a set of characteristics that make them unique
among the object classes that you encounter in Compact Framework programming. These
characteristics also make Compact Framework forms somewhat different from desktop forms.
What makes forms unique is that they: [Comment 5cs.2]
Are derived, directly or indirectly, from System.Windows.Forms.Form [Comment 5cs.3]
All inherited events are supported [Comment 5cs.4]
On a Pocket PC, are displayed full-screen and are not resizable [Comment 5cs.5]
Can be displayed as modal or modeless forms [Comment 5cs.6]
On a Pocket PC, present the user with a mechanism for closing or minimizing the form, but
Can be closed but not opened [Comment 5cs.8]
Are indestructible until they have been closed [Comment 5cs.9]
Are worthless after they have been closed [Comment 5cs.10]
Are owned by a thread [Comment 5cs.11]
Chapter 5 – Creating Forms
Page 1
Copyright © 2003 Paul Yao & David Durant
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Forms are Derived from Form
Like everything else in .NET that is not a value type, forms are objects. Normally you derive
your forms directly from the Form class that is defined in the System.Windows.Forms
namespace. This is the default derivation whenever you add a new form to the project. Listing 5-
1 shows the code file that resulted when adding a form named FormDemo to a project. [Comment
Listing 5-1 – The code that defines a new form [Comment 5cs.13]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace MainPlusOthers
{
/// <summary>
/// Summary description for FormDemo.
/// </summary>
public class FormDemo : System.Windows.Forms.Form
{
:
:
The first few lines of code leave no doubt that you are defining an object class, one that
inherits from Form . [Comment 5cs.14]
Optionally, your form classes can be derived from other form classes, classes that were
themselves derived from Form . For further discussion of deriving forms from other forms, see
the Inheritance section later in this chapter. [Comment 5cs.15]
Properties, Methods, and Events
Because your forms are derived from the base class of Form , they inherit the properties,
methods, and events of the Form class. The Form class itself obtains most of its properties,
methods, and events through inheritance, with an inheritance chain that starts at Object and
descends through the following classes: MarshalByRefObject , Component , Control ,
ScrollableControl , and ContainerControl . Tables 5-1 through 5-3 list the properties,
methods, and events, respectively, of the Form class. Those that are form-specific, this is, ones
that are defined within the Form class rather than being inherited, are marked with an asterisk
“(*)”. For the reader convenience we have broken them down into categories of our own
choosing. Chapter 7, Inside Controls , provides additional information about the Control class,
which is the base class for all control classes which wrap around a native operating system
Table 5-1 - Form Propertie s [Comment 5cs.17]
Category
Property Name
Comments
Pocket PC-Specific Comments
Ownership
Parent
For dialog boxes, do not set a
parent.
Inherited from Control , Parent is
normally null because forms do not
have a parent. When a form is a dialog
box, however, its parent is the form that
spawned the dialog (the parent is
disabled).
Chapter 5 – Creating Forms
Page 2
Copyright © 2003 Paul Yao & David Durant
870585273.058.png 870585273.069.png 870585273.080.png 870585273.001.png 870585273.002.png 870585273.003.png 870585273.004.png 870585273.005.png 870585273.006.png 870585273.007.png 870585273.008.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Category
Property Name
Comments
Pocket PC-Specific Comments
Controls
Collection containing the controls that
reside on the form
TopLevelControl Inherited from Control, provides
immediate access to the form which
contains a set of controls.
State
Capture
Boolean indicating whether form should
grab all mouse / stylus input.
A form automatically sets
mouse capture for a mouse
down event, and releases the
mouse capture for a mouse up
event.
Enabled
Indicates whether form can receive
keyboard and mouse input. When a form
is disabled, all controls contained in the
form are disabled (see chapter 6, Mouse
and Keyboard Input )
Focused
Boolean indicating whether keyboard
input is directed to the form.
Visible
Boolean indicating whether form and its
child controls are on the desktop, and
whether the top-level form appears on the
task list or not.
WindowState (*) Whether form is initially full-screen
(maximized) or not.
Ignored – forms run full-screen.
Appearance
BackColor
Color used to draw form surface
ControlBox (*) Presence of close button (when set to
false, prevents minimize-box and
maximize-box from appearing). When
run on desktop framework, controls
whether system menu appears (The
Windows CE user-interface does not
support a system menu.)
Whether a form has a close box
or Ok button. (Pocket PC
programs have no close button.)
FormBorderStyle
(*)
Sets type of border and whether form is
resizeable. (CF programs running under
the desktop framework vary slightly from
the same program running under CE.)
Ignored.
Icon (*)
Ignored on Pocket PC and Windows CE.
(Appears only running on the desktop.)
Set an application icon from the Visual
Studio project properties page.
Ignored.
MaximizeBox (*) Whether maximize box appears (requires
ControlBox = true)
Ignored.
MinimizeBox (*) Whether minimize box appears (requires
ControlBox = true)
When true , close box appears,
which when clicked causes form
to continue running.
When false , Ok box appears,
which when clicked causes form
to close and program to
terminate.
Text
Text for form's caption bar.
Menus
Menu (*)
A form's main menu.
Access to input panel (SIP)
requires a menu or a toolbar.
(There is no dedicated toolbar
property, but toolbars are added
to a form's control collection.)
Chapter 5 – Creating Forms
Page 3
Copyright © 2003 Paul Yao & David Durant
870585273.009.png 870585273.010.png 870585273.011.png 870585273.012.png 870585273.013.png 870585273.014.png 870585273.015.png 870585273.016.png 870585273.017.png 870585273.018.png 870585273.019.png 870585273.020.png 870585273.021.png 870585273.022.png 870585273.023.png 870585273.024.png 870585273.025.png 870585273.026.png 870585273.027.png 870585273.028.png 870585273.029.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Category
Property Name
Comments
Pocket PC-Specific Comments
ContextMenu
A form's context menu.
Dialogs
DialogResult
(*)
Return value for dialog boxes.
Position & Size
Bottom
(read-only) X-coordinate for bottom of
window in parent's client area (or in
screen coordinates for top-level
windows).
Bounds
A Rectangle equal to ( Left , Top ,
Right , Bottom ) in parent's client area
coordinates for contained controls, and in
screen-coordinates for top-level forms.
ClientRectangle (read-only) A Rectangle equal to ( 0 ,
0 , ClientSize.Width ,
ClientSize.Height )
ClientSize
Size minus non-client elements.
Form height (including non-client
elements).
Height
Left
Y-axis location in parent (or screen)
coordinates.
Location
A Point equal to ( Left , Top )
Read-only
Right
Size
A Size equal to ( Width , Height )
Top
X-axis location in parent (or screen)
coordinates.
Width
Form width (including non-client
elements).
DataBinding
DataBindings
Collection of data binding objects.
(*) Specific to
Form class
Several of the properties are related to size and position, and you want to leave them as is.
On the Pocket PC, forms are displayed full-screen. But if you accidentally change the size, the
layout of controls in the forms designer might not be the same as what the user will see on a
Pocket PC. To ensure that you do not accidentally modify the size properties, use the Lock
Controls option. Right click on the form in Design View and select Lock Controls from the
context menu. [Comment 5cs.18]
The value of the form’s MinimizeBox property determines whether the form displays a
minimize box or a close box to the user. The impact of this choice is discussed in the Orphaned
Forms section of this chapter. [Comment 5cs.19]
The value of the form’s ControlBox property determines whether the form displays a control
box in the upper left corner of the form. The presence of a control box allows the user to display
the system menu. [Comment 5cs.20]
The Data Bindings collection is covered in Chapter 8, Data Binding , and is revisited in the
next chapter. [Comment 5cs.21]
The DialogResult property is most applicable to forms that are displayed as dialogs, and is
addressed in chapter 9, Inside More Controls . [Comment 5cs.22]
Chapter 5 – Creating Forms
Page 4
Copyright © 2003 Paul Yao & David Durant
870585273.030.png 870585273.031.png 870585273.032.png 870585273.033.png 870585273.034.png 870585273.035.png 870585273.036.png 870585273.037.png 870585273.038.png 870585273.039.png 870585273.040.png 870585273.041.png 870585273.042.png 870585273.043.png 870585273.044.png 870585273.045.png 870585273.046.png 870585273.047.png 870585273.048.png 870585273.049.png 870585273.050.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
In the current version of the Compact Framework 1 , the icon property is not used. This
property accepts an icon created from a managed resource (in a .resx file). Such an icon can
only be loaded as a managed System.Drawing.Icon object. However, the user-interface shell
of both Pocket PC and Windows CE devices require a native (unmanaged) icon resource. Add a
native icon to a program in the Property Pages for a project by specifying the application icon.
The icon (*.ico) file format can accommodate more than one icon. When creating an icon for
a Compact Framework program, be sure that your icon supports two icons: a 16x16 image as
well as a 32x32 image. The smaller image appears on the Start menu; the larger image appears
in the program window. [Comment 5cs.24]
Table 5-2 - Form Method s [Comment 5cs.25]
Category
Method Name
Comments
Dialog Box Support
ShowDialog (*)
Displays a form as a dialog box, disabling the form's parent.
CreateGraphics
Fetches a Graphic object for drawing on the surface of the form.
Drawing
Invalidate
Mark a portion of a form for lazy redrawing (through Paint event).
Refresh
Immediate redraw of form and its controls (like call to Invalidate
and Update on form and on all contained controls)
Update
Immediate redraw of invalid portions.
PointToClient
Convert a point from screen coordinates to client coordinates.
Coordinate Conversion
PointToScreen
Convert a point from client coordinates to screen coordinates.
RectangleToClient
Convert a rectangle from screen coordinates to client coordinates.
RectangleToScreen
Convert a rectangle from client coordinates to screen coordinates.
Inherited Type Support
Equals
Checks a form for equality with another form.
GetType
Returns System.Windows.Forms.Form
ToString
Returns a string with the fully-qualified name of a form class
Invoke
Thread-safe access
Calls a delegate in a form (or other Control -derived class) in a
thread-safe manner. Thread-safe, by the way, does not mean the call
must be made on a separate thread, a point of confusion with
programmers new to Windows thread programming.
State
Close (*)
Requests that a form be closed.
Dispose
Requests that resources associated with a form be reclaimed.
Focus
Sets keyboard focus to a form.
Hide
Makes the form, and all contained controls, invisible.
Show
Allows a form and all contained controls to be visible.
Z-Order
BringToFront
Moves a form to the top of the local-input Z-order. The Compact
Framework does not support setting a form at the top of the global Z-
order; that requires calling the native SetForegroundWindow
function.
SendToBack
Moves a form to the bottom of the local-input Z-order.
(*) Specific to
Form class
The Show and Hide methods are equivalent to setting the Visible property to True or
False , respectively. Calling BringToFront or SendToBack results in a form being placed at the
top or bottom of the Z-order, respectively. [Comment 5cs.26]
1 Just before this book went to press, we tested this on the Compact Framework, version 1.0, SP-2.
Chapter 5 – Creating Forms
Page 5
Copyright © 2003 Paul Yao & David Durant
870585273.051.png 870585273.052.png 870585273.053.png 870585273.054.png 870585273.055.png 870585273.056.png 870585273.057.png 870585273.059.png 870585273.060.png 870585273.061.png 870585273.062.png 870585273.063.png 870585273.064.png 870585273.065.png 870585273.066.png 870585273.067.png 870585273.068.png 870585273.070.png 870585273.071.png 870585273.072.png 870585273.073.png 870585273.074.png 870585273.075.png 870585273.076.png 870585273.077.png 870585273.078.png 870585273.079.png
 
Zgłoś jeśli naruszono regulamin