.NET Windows Forms in a Nutshell

Windows provides several kinds of widely used controls, such as buttons and text boxes, which act as the fundamental building blocks in most user interfaces. All these standard control types have .NET equivalents. This section shows which controls are available and what their Win32 equivalents are. It also describes some of the issues common to all the standard controls. More detailed technical descriptions of each control can be found in the reference section.

2.3.1 Available Controls

Table 2-1 shows the list of available controls and the nearest equivalent window class in Win32. (Some Win32 classes, such as Button , have several different modes, each of which is represented by a different class in Windows Forms. In this case, a Win32 window style is also specified to indicate which particular flavor of this class the relevant .NET type represents.)

Table 2-1. .NET controls and their equivalent Win32 control classes

Control class

Equivalent Win32 window class (and style)

Purpose

Buttons

Button

Button (BS_PUSHBUTTON)

Normal button for actions (e.g., OK or Cancel)

CheckBox

Button (BS_CHECKBOX)

Yes/no selection button

RadioButton

Button (BS_RADIOBUTTON)

Single selection from a range of choices

Labels and pictures

GroupBox

Button (BS_GROUPBOX)

Visual grouping for sets of related controls

Label

Static (SS_LEFT, SS_CENTER, SS_RIGHT)

Text label, usually providing a name or description for some other control (e.g., a text box)

PictureBox

Static (SS_BITMAP, SS_ICON or SS_ENHMETAFILE)

A picture: supports various bitmap formats (BMP, ICO, JPEG, TIFF, and PNG) and Windows metafiles

LinkLabel

SysLink

Hyperlink, e.g., a URL; this effectively combines label-like and button-like behavior

Text editing

TextBox

Edit

An editable text field (plain text only)

RichTextBox

RichEdit20W/ RichEdit20A

An editable text fields supporting text with formatting (based on RTFthe Rich Text Format)

NumericUpDown

msctls_updown32

A text box containing a number, and an associated pair of up/down buttons (often known as a spin control)

DomainUpDown

 

Similar to a NumericUpDown , only the text box can contain any string; the up and down buttons move through a list of strings

Time and date

DateTimePicker

SysDateTimePick32

UI for specifying a date or time

MonthCalendar

SysMonthCal32

UI showing a single calendar month

Lists and data

ListBox

ListBox

A vertical list of selectable text items (items may also have images)

ComboBox

ComboBox

An editable text field with an associated drop-down list of selectable items

ListView

SysListView32

A list of selectable items similar to the contents of a Windows Explorer window; supports Large Icon, Small Icon, List and Details views

TreeView

SysTreeView

A hierarchical display, similar to that used in the Folders pane of Windows Explorer

PropertyGrid

 

A UI for editing properties on some object; very similar to the Properties panels in Visual Studio .NET

DataGrid

 

A grid control showing the contents of a DataSet

Position and progress bars

HScrollBar

ScrollBar

A horizontal Windows scrollbar

VScrollBar

ScrollBar

A vertical Windows scrollbar

TrackBar

msctls_trackbar32

A UI for selecting from a linear range of values (useful for continuous ranges such as percentages)

ProgressBar

msctls_progress32

A bar indicating what proportion of a long-running task has completed

Layout

TabControl

SysTabControl32

Allows multiple similarly sized dialogs to share a single window, with card index style tabs selecting between themsimilar to those used on Properties pages in Windows Explorer

Splitter

 

A bar dividing two parts of a window either vertically or horizontally, allowing the proportion of space given to the two parts to be modifiedsimilar to the divider between the Folders pane and the main pane of a Windows Explorer window

StatusBar

msctls_statusbar32

A bar along the bottom of the window providing textual information appropriate to the application, and a window resizing grip (most Windows applications have these)

ToolBar

ToolbarWindow32

A bar containing shortcut buttons to frequently used UI operations (most Windows applications have these)

Note that some controls don't have an equivalent Win32 window classthe Windows Forms class library adds some new features. There are also some Win32 controls that appear to be absent, but in most cases, this is because their roles can be filled by one of the other controls. For example, Windows Forms does not provide a direct replacement for the Animation control, but this is because the PictureBox control supports animated bitmaps. There is also no ToolTip control, but ToolTips are dealt with through a different mechanism, called an extender property .

The Win32 DragList , Header , and Pager control types don't have any equivalent in Windows Forms.

2.3.2 Using the Built-in Controls

Not all the features supported by the Control class make sense for certain controls. For example, the GroupBox control does not respond to mouse eventsits only purpose is to provide a visual grouping for controls. Fortunately, if you use Visual Studio .NET it will only present the features supported by the controls you use. (Chapter 5 explains how to determine which features are enabled for any custom controls you write, and Chapter 8 shows how to control the way in which Visual Studio. NET presents these features.)

Some controls ( group boxes, labels, and all three button types) support a property called FlatStyle . This property modifies the way controls are drawn. By default, it is set to Flat.Standard , which means the control is drawn by the Windows Forms class library and not by the underlying OS. While this means that extra nonstandard functionality is available (e.g., the ability to set background colors or images), it has the disadvantage that your application will not be able to take advantage of themed controls. So in Windows XP, buttons will come out looking like normal Windows 2000 buttons regardless of what theme the user may be running with. If you would like to have themed controls (and don't mind losing support for background color and bitmaps), you must set the FlatStyle property to FlatStyle.System . [2] Although this means that Windows Forms will now let the operating system draw the controls, this in itself is not enough to get themed controlsas with any theme-aware application, you must also supply a manifest file. (Application manifests in .NET are used in exactly the same way as they are for non-.NET programs. They also have nothing to do with .NET assembly manifests. Consult the Win32 SDK documentation for details on how to create and use such manifests .)

[2] To complicate matters further, some controls, e.g., TextBox , use system drawing in any case. These controls don't support background bitmaps and colors, so they never need to draw themselves .

Категории