Documente Academic
Documente Profesional
Documente Cultură
13
Graphical User
Interface Concepts:
Part I
2006 Pearson Education, Inc. All rights reserved.
2
OBJECTIVES
In this chapter you will learn:
Design principles of graphical user interfaces (GUIs).
How to create graphical user interfaces.
How to process events that are generated by user
interactions with GUI controls.
The namespaces that contain the classes for
graphical user interface controls and event handling.
How to create and manipulate Button, Label,
RadioButton, CheckBox, TextBox, Panel and
NumericUpDown controls.
How to add descriptive ToolTips to GUI controls.
How to process mouse and keyboard events.
13.1 Introduction
13.2 Windows Forms
13.3 Event Handling
13.3.1 A Simple Event-Driven GUI
13.3.2 Another Look at the Visual Studio Generated
Code
13.3.3 Delegates and the Event-Handling Mechanism
13.3.4 Other Ways to Create Event Handlers
13.3.5 Locating Event Information
13.4 Control Properties and Layout
13.5 Labels, TextBoxes and Buttons
13.6 GroupBoxes and Panels
13.7 CheckBoxes and RadioButtons
13.8 PictureBoxes
13.9 ToolTips
13.10 NumericUpDown Control
13.11 Mouse-Event Handling
13.12 Keyboard-Event Handling
13.13 Wrap-Up
13.1 Introduction
Control Description
• Windows Forms
– Used to create GUIs for programs
– Graphical element that appears on your computer’s
desktop
– Active window is the front most window
– A Form is a container for controls and components
– In visual programming, Visual Studio generates much of
the GUI-related code
Display all
controls and
components
Categories that
organize
controls and
components by
functionality
Common Properties
AcceptButton Button that is clicked when Enter is pressed.
AutoScroll Boolean value that allows or disallows scrollbars when needed.
CancelButton Button that is clicked when the Escape key is pressed.
FormBorderStyle Border style for the Form (e.g., none, single, three-dimensional).
Font Font of text displayed on the Form, and the default font for controls added to the
Form.
Text Text in the Form’s title bar.
Common Methods
Close Closes a Form and releases all resources, such as the memory used for the Form’s
controls and components. A closed Form cannot be reopened.
Hide Hides a Form, but does not destroy the Form or release its resources.
Common Event
Load Occurs before a Form is displayed to the user. The handler for this event is
displayed in the Visual Studio editor when you double click the Form in the Visual
Studio designer.
• Event Handling
– GUIs are event driven
– When user interacts with a GUI component, the
interaction is known as an event
– A method that performs a task in response to an event is
called an event handler
Fig. 13.6 | First half of the Visual Studio generated code file.
Fig. 13.7 | Second half of the Visual Studio generated code file.
Events icon
Selected events
Fig. 13.8 | Viewing events for a Button control in the Properties window.
Event name
Event type
Event argument
class
Common Properties
BackColor The control’s background color.
BackgroundImage The control’s background image.
Enabled Specifies whether the control is enabled (i.e., if the
user can interact with it). Typically, portions of a
disabled control appear “grayed out” as a visual
indication to the user that the control is disabled.
Focused Indicates whether the control has the focus.
Font The Font used to display the control’s text.
ForeColor The control’s foreground color. This usually
determines the color of the text in the Text
property.
TabIndex The tab order of the control. When the Tab key is
pressed, the focus transfers between controls based
on the tab order. You can set this order.
TabStop If true, then a user can give focus to this control
via the Tab key.
Class Control
properties and
methods Description
TabStop If true, then a user can give focus to this control via
the Tab key.
Text The text associated with the control. The location and
appearance of the text vary depending on the type of
control.
Common Methods
Anchoring
window
Click down-arrow
in Anchor
property to display
anchoring window
Constant distance
to right and bottom
sides
After resizing
Before resizing
Control layout
properties Description
Common Label
properties Description
TextBox properties
and events Description
Common Properties
AcceptsReturn If true in a multiline TextBox, pressing Enter in the TextBox creates a
new line. If false, pressing Enter is the same as pressing the default
Button on the Form. The default Button is the one assigned to a Form’s
AcceptButton property.
Multiline If true, the TextBox can span multiple lines. The default value is
false.
PasswordChar When this property is set to a character, the TextBox becomes a
password box, and the specified character masks each character the user
type. If no character is specified, the TextBox displays the typed text.
ReadOnly If true, the TextBox has a gray background, and its text cannot be
edited. The default value is false.
ScrollBars For multiline textboxes, this property indicates which scrollbars appear
(None, Horizontal, Vertical or Both).
Text The TextBox’s text content.
Common Event
TextChanged Generated when the text changes in a TextBox (i.e., when the user adds
or deletes characters). When you double click the TextBox control in
Design mode, an empty event handler for this event is generated.
Button properties
and events Description
Common Properties
Control inside
Panel
Panel
Panel
Scrollbars
Panel
resized
Common Properties
Checked Indicates whether the CheckBox is checked (contains a check mark) or unchecked (blank).
This property returns a Boolean value.
CheckState Indicates whether the CheckBox is checked or unchecked with a value from the
CheckState enumeration (Checked, Unchecked or Indeterminate). Indeterminate
is used when it is unclear whether the state should be Checked or Unchecked. For example,
in Microsoft Word, when you select a paragraph that contains several character formats, then
go to Format > Font, some of the CheckBoxes appear in the Indeterminate state. When
CheckState is set to Indeterminate, the CheckBox is usually shaded.
Text Specifies the text displayed to the right of the CheckBox.
Common Events
CheckedChanged Generated when the Checked property changes. This is a CheckBox’s default event. When a
user double clicks the CheckBox control in design view, an empty event handler for this event
is generated.
CheckStateChanged Generated when the CheckState property changes.
RadioButton
properties and events Description
Common Properties
Checked Indicates whether the RadioButton is checked.
Text Specifies the RadioButton’s text.
Common Event
CheckedChanged Generated every time the RadioButton is
checked or unchecked. When you double click a
RadioButton control in design view, an empty
event handler for this event is generated.
RadioButtonsTest
Form.cs
(6 of 7)
RadioButtonsTest
Form.cs
(7 of 7)
13.8 PictureBoxes
•PictureBoxes
– Display an image
PictureBox
properties and event Description
Common Properties
Image Sets the image to display in the PictureBox.
SizeMode Enumeration that controls image sizing and
positioning. Values are Normal (default),
StretchImage, AutoSize and
CenterImage. Normal places the image in
the top-left corner of the PictureBox, and
CenterImage puts the image in the middle.
These two options truncate the image if it is too
large. StretchImage resizes the image to fit
in the PictureBox. AutoSize resizes the
PictureBox to hold the image.
Common Event
Click Occurs when the user clicks the control. When
you double click this control in the designer, an
event handler is generated for this event.
(b)
PictureBoxTestForm
.cs
(2 of 2)
(c)
13.9 ToolTips
•ToolTips
– Helpful text that appears when the mouse hovers over an
item
ToolTip properties
and events Description
Common Properties
AutoPopDelay The amount of time (in milliseconds) that the tool tip
appears while the mouse is over a control.
InitialDelay The amount of time (in milliseconds) that a mouse must
hover over a control before a tool tip appears.
ReshowDelay The amount of time (in milliseconds) between which
two different tool tips appear (when the mouse is moved
from one control to another).
Common Event
Draw Raised when the tool tip is displayed. This event allows
programmers to modify the appearance of the tool tip.
(a) (b)
ToolTip in
component tray
Property to set
tool tip text Tool tip text
•NumericUpDown
– Restrict a user’s input choices to a specific range of
numeric values.
– Appears as a TextBox, with two small Buttons on the
right side
– NumericUpDown’s ReadOnly property indicates if user
can type a number into the control
NumericUpDown
properties and event Description
Common Properties
Increment Specifies by how much the current number in
the control changes when the user clicks the
control’s up and down arrows.
Maximum Largest value in the control’s range.
Minimum Smallest value in the control’s range.
UpDownAlign Modifies the alignment of the up and down
Buttons on the NumericUpDown control.
This property can be used to display these
Buttons either to the left or to the right of
the control.
Value The numeric value currently displayed in the
control.
Common Event
ValueChanged This event is raised when the value in the
control is changed. This is the default event
for the NumericUpDown control.
NumericalUpDown
control
Click to decrease
number of years
• Mouse-Event Handling
– Mouse events can be handled for any control that derives
from class System.Windows.Forms.Control
– Class MouseEventArgs
• Contains information related to the mouse event
• Information about the event is passed to the event-handling
method through an object of this class
– The delegate used to create the mouse-event handlers is
MouseEventHandler
• Keyboard-Event Handling
– Key events occur when keyboard keys are pressed and released
– There are three key events:
• KeyPress
– The event occurs when the user presses a key that represents
an ASCII character
• The specific key can be determined with property
KeyChar of the event handler’s KeyPressEventArgs
argument
• Does not indicate whether modifier keys were pressed
• KeyUp and KeyDown
– If information about the modifier keys are important, use the
KeyUp or KeyDown events
• The KeyEventArgs argument for each of these events
contains information about modifier keys.
KeyDemoForm.cs
(b) F12 pressed
(3 of 3)
(c) $ pressed