Chapter12_PPT.ppt

Download Report

Transcript Chapter12_PPT.ppt

Chapter 12 – Graphical User Interface
Concepts: Part 1
Outline
12.1
12.2
12.3
12.4
12.5
12.6
12.7
12.8
12.9
12.10
Introduction
Windows Forms
Event-Handling Model
12.3.1 Basic Event Handling
Control Properties and Layout
Labels, TextBoxes and Buttons
GroupBoxes and Panels
CheckBoxes and RadioButtons
PictureBoxes
Mouse-Event Handling
Keyboard-Event Handling
 2002 Prentice Hall. All rights reserved.
1
2
12.1 Introduction
• GUI
– Graphical User Interface
• Allows visual interaction
• Event driven
– User interaction generates events
• Distinctive “look” and “feel”
• Learn new applications more quickly
 2002 Prentice Hall. All rights reserved.
3
12.1 Introduction
Fig. 12.1 Sample Internet Explorer window with GUI components.
 2002 Prentice Hall. All rights reserved.
4
12.1 Introduction
• GUI Components
– Objects with which user interacts
• Event generation
– Contained in Toolbox
• Control
– Component with graphical representation
 2002 Prentice Hall. All rights reserved.
5
12.1 Introduction
Comp o ne nt
De sc rip tio n
Label
An area in which icons or uneditable text can be displayed.
Textbox
An area in which the user inputs data from the keyboard. This area
also can display information.
Button
An area that triggers an event when clicked.
CheckBox
A component that is either selected or not selected.
ComboBox
A drop-down list of items from which the user can make a selection
either by clicking an item in the list or by typing into a box.
ListBox
An area in which a list of items is displayed. The user can make a
selection from the list by clicking on any element. Multiple elements
can be selected.
Panel
A container in which components can be placed.
Scrollbar
A component that allows the user to access a range of values that
normally cannot fit in the controller’s container.
Fig. 12.2
Som e b a sic GUI c om p one nts.
Fig. 12.2 Some basic GUI components.
 2002 Prentice Hall. All rights reserved.
6
12.2 Windows Forms
• Form
– Graphical element used to create GUIs
– Click and drag component from Toolbox
• Code generated
– Component is instantiated
– Basic properties are set
 2002 Prentice Hall. All rights reserved.
7
12.2 Windows Forms
Fig. 12.3 Components and controls for Windows Forms.
 2002 Prentice Hall. All rights reserved.
8
12.2 Windows Forms
Form Prop ertie s a nd
De sc rip tio n / De le g a te a nd Event Arg ume nts
Eve nts
Common
Properties
AcceptButton
AutoScroll
CancelButton
FormBorderStyle
Button that is clicked when Enter is pressed.
Boolean value that allows or disallows the use of scrollbars to appear
when needed.
Button that is clicked when the Escape key is pressed.
Border of the form (e.g., none, single, 3D, sizable).
Font
Font of text displayed on the form, and the default font of controls
added to the form.
Text
Text in the form’s title bar.
Common Methods
Close
Closes a form and releases all resources. A closed form cannot be
reopened.
Hide
Hides form (does not release resources).
Show
Displays a hidden form.
Common Events
(Delegate EventHandler, event arguments
EventArgs)
Load
Occurs before a form is shown. This event is the default when the
form is double-clicked in the Visual Studio .NET designer.
Fig. 12.4
Com m o n
Form p rop erties a nd e vents.
Fig. 12.4 Common Form properties and events.
 2002 Prentice Hall. All rights reserved.
9
12.3 Event-Handling Model
• Event handler
– Method called by event
– Receives event information
• Notion of delegates
– Objects that reference methods
– Act as intermediaries between objects creating events and
methods handling events
• Event multicasting
– Inclusion of multiple handlers for one event
 2002 Prentice Hall. All rights reserved.
10
12.3 Event-Handling Model
calls
Handler 1 for event E
calls
Object A raises event E
Delegate for event E
Handler 2 for event E
Handler 3 for event E
Fig. 12.5 Event-handling model using delegates.
 2002 Prentice Hall. All rights reserved.
11
12.3 Event-Handling Model
Fig. 12.6 Events section of the Properties window.
 2002 Prentice Hall. All rights reserved.
12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
' Fig. 12.7: SimpleEventExample.vb
' Program demonstrating simple event handler.
Public Class FrmSimple
Inherits System.Windows.Forms.Form
Outline
SimpleEvenExampl
e.vb
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
Beginning of Visual Studio
generated code
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the
' InitializeComponent() call
End Sub ' New
' Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose( _
ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub ' Dispose
Friend WithEvents lblOutput As System.Windows.Forms.Label
 2002 Prentice Hall.
All rights reserved.
13
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
' Required by the Windows Form Designer
Private components As System.ComponentModel.Container
' NOTE: The following procedure is required by
' the Windows Form Designer.
' It can be modified using the Windows Form Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.lblOutput = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'lblOutput
'
Me.lblOutput.Location = New System.Drawing.Point(32, 48)
Me.lblOutput.Name = "lblOutput"
Me.lblOutput.Size = New System.Drawing.Size(168, 40)
Me.lblOutput.TabIndex = 0
Me.lblOutput.Text = "Click Me!"
'
'FrmSimple
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(272, 237)
Me.Controls.AddRange( _
New System.Windows.Forms.Control() {Me.lblOutput})
Outline
SimpleEvenExampl
e.vb
Me.Name = "FrmSimple"
Me.Text = "SimpleEventExample"
Me.ResumeLayout(False)
End
End Sub
#End Region
of Visual Studio
generated code
 2002 Prentice Hall.
All rights reserved.
14
71
72
73
74
75
76
77
78
' handler for click event on lblOutput
Private Sub lblOutput_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles lblOutput.Click
MessageBox.Show("Label was pressed")
End Sub ' lblOutput_Click
End Class ' FrmSimpleExample
Name of the handler
followed by an underscore
and the event name
Outline
SimpleEvenExampl
Reference to the object e.vb
that generated the event
Event arguments object
Event-handling code
displays a message box
 2002 Prentice Hall.
All rights reserved.
15
12.3 Event-Handling Model
Fig. 12.8 List of Form events.
 2002 Prentice Hall. All rights reserved.
16
12.3 Event-Handling Model
Fig. 12.9 Details of Click event.
 2002 Prentice Hall. All rights reserved.
17
12.4 Control Properties and Layout
De sc rip tio n
Cla ss Control
Prop erties a nd Metho d s
Common
Properties
BackColor
Sets the background color of the control.
BackgroundImage
Sets the background image of the control.
Enabled
Indicates whether the control is enabled (i.e., if the user can interact
with it). A disabled control will still be displayed, but portions of the
control will appear in gray.
Focused
Indicates whether a control has the focus.
Font
Sets the Font to use in Text property of control.
ForeColor
Sets the foreground color of the control. This is usually the color of
the Text property.
TabIndex
Sets the tab order of the control. When the Tab key is pressed, the
focus is moved to various controls according to the tab order. This
order can be set by the programmer.
TabStop
Indicates whether user can employ the Tab key to select the control.
If True, then this feature is enabled.
Sets the text associated with the control. The location and appearance
varies depending on the type of control.
Text
TextAlign
Establishes the alignment of the text on the control—possibilities are
one of three horizontal positions (left, center or right) and one of three
vertical positions (top, middle or bottom).
Visible
Indicates whether the control is visible.
Common Methods
Focus
Transfers the focus to the control.
Hide
Hides the control (sets Visible to False).
Show
Shows the control (sets Visible to True).
Fig. 12.10 Cla ss Control p rop erties a nd m ethod s.
Fig. 12.10 Class Control properties and methods.
 2002 Prentice Hall. All rights reserved.
18
12.4 Control Properties and Layout
• Method Focus
– Transfers focus to control
• Active control
• Method Hide
– Hides control
• Visible property is false
• Method Show
– Shows control
• Visible property is true
 2002 Prentice Hall. All rights reserved.
19
12.4 Control Properties and Layout
• Anchoring
– Specifying layout of controls within container
– Controls remain fixed distances from inside of container
• Docking
– Sets dimensions of control to dimensions of container at all
times
 2002 Prentice Hall. All rights reserved.
20
12.4 Control Properties and Layout
Fig. 12.11 Anchoring demonstration.
 2002 Prentice Hall. All rights reserved.
21
12.4 Control Properties and Layout
Fig. 12.12 Manipulating the Anchor property of a control.
 2002 Prentice Hall. All rights reserved.
22
12.4 Control Properties and Layout
Fig. 12.13 Docking demonstration.
 2002 Prentice Hall. All rights reserved.
23
12.4 Control Properties and Layout
C om m on La yo ut
Prop erties
De sc rip tio n
Anchor
Attaches control to the side of parent container. Used during resizing.
Possible values include top, left and right.
Dock
Allows controls to extend themselves along the sides of their
containers—values cannot be combined.
DockPadding (for
containers)
Sets the dock spacing for controls inside the container. Default is zero,
causing controls to appear flush with the side of the container.
Location
Specifies the location of the upper-left corner of the control, in
relation to its container.
Size
Specifies the size of the control. Takes a Size structure, which has
properties Height and Width.
MinimumSize,
MaximumSize (for
Windows Forms)
Indicates the minimum and maximum size of the form.
Fig. 12.14
Control la yout p rop erties.
Fig. 12.14 Control layout properties.
 2002 Prentice Hall. All rights reserved.
24
12.5 Labels, TextBoxes and Buttons
• Label
– Displays read-only text
• Textbox
– Displays text
– Text input by user
• Button
– Click to trigger action
 2002 Prentice Hall. All rights reserved.
25
12.5 Labels, TextBoxes and Buttons
Common Label
De sc rip tio n / De le g a te a nd Event Arg ume nts
Prop erties
Font
The font used by the text on the Label.
Text
The text that appears on the Label.
TextAlign
The alignment of the Label’s text on the control. Possibilities are
one of three horizontal positions (left, center or right) and one
of three vertical positions (top, middle or bottom).
Fig. 12.15 Com m o n
Label p rop e rties.
Fig. 12.15 Common Label properties.
 2002 Prentice Hall. All rights reserved.
26
12.5 Labels, TextBoxes and Buttons
TextBox Prop erties
De sc rip tio n / De le g a te a nd Event Arg ume nts
a nd Events
Common
Properties
AcceptsReturn
If True, pressing Enter creates a new line (if textbox is configured
Multiline
to contain multiple lines.) If False, pressing Enter clicks the
default button of the form.
If True, textbox can span multiple lines. The default value is False.
PasswordChar
If a character is entered, the TextBox becomes a password box, and
the stated character replaces all characters typed. If no character is
specified, Textbox displays the typed text.
ReadOnly
If True, TextBox has a gray background, and its text cannot be
edited. The default value is False.
ScrollBars
For multiline textboxes, indicates which scrollbars appear (none,
horizontal, vertical or both).
Text
Specifies the text to be displayed in the text box.
Common Events
(Delegate EventHandler, event arguments
EventArgs)
TextChanged
Raised when text changes in TextBox (i.e., when the user adds or
deletes characters). When a user double-clicks the TextBox control
in design view, an empty event handler for this event is generated.
Fig. 12.16
TextBox p rop e rties a nd events.
Fig. 12.16 TextBox properties and events.
 2002 Prentice Hall. All rights reserved.
27
12.5 Labels, TextBoxes and Buttons
Button p rop erties a nd
De sc rip tio n / De le g a te a nd Event Arg ume nts
eve nts
Common
Properties
Text
Specifies text displayed on the Button face.
Common Events
(Delegate EventHandler, event arguments
EventArgs)
Click
Is raised when user clicks the control. When a user double-clicks on
the Button control in design view, an empty event handler for this
event is generated.
Fig. 12.17
Button p rop erties a nd events.
Fig. 12.17 Button properties and events.
 2002 Prentice Hall. All rights reserved.
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Outline
' Fig. 12.18: LabelTextBoxButtonTest.vb
' Using a textbox, label and button to display the hidden
' text in a password box.
LabelTextBoxButt
onTest.vb
Public Class FrmButtonTest
Inherits System.Windows.Forms.Form
' Visual Studio .NET generated code
' handles cmdShow_Click events
Private Sub cmdShow_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdShow.Click
lblOutput.Text = txtInput.Text
End Sub ' cmdShow_Click
End Class ' FrmButtonTest
The label’s text property is set
equal to the value of the textbox’s
text property, which was
entered by the user
 2002 Prentice Hall.
All rights reserved.
29
12.6 GroupBoxes and Panels
• Groupboxes
– Arrange controls on a GUI
– Can display captions
– Do not include scrollbars
• Panels
– Arrange controls on a GUI
– Cannot display captions
– Include scrollbars
 2002 Prentice Hall. All rights reserved.
30
12.6 GroupBoxes and Panels
GroupBox Prop erties
De sc rip tio n
Controls
Lists the controls that the GroupBox contains.
Text
Specifies text displayed on the top portion of the GroupBox (its
caption).
Fig. 12.19
GroupBox p rop erties.
Fig. 12.19 GroupBox properties.
Panel Prop erties
De sc rip tio n
AutoScroll
Indicates whether scrollbars appear when the Panel is too small to
hold its controls. Default is False.
BorderStyle
Sets the border of the Panel (default None; other options are
Fixed3D and FixedSingle).
Controls
Lists the controls that the Panel contains.
Fig. 12.20
Panel p rop e rties.
Fig. 12.20 Panel properties.
 2002 Prentice Hall. All rights reserved.
31
12.6 GroupBoxes and Panels
Fig. 12.21 Creating a Panel with scrollbars.
 2002 Prentice Hall. All rights reserved.
32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
' Fig. 12.22: GroupBoxPanelExample.vb
' Using GroupBoxes and Panels to hold buttons.
Public Class FrmGroupBox
Inherits System.Windows.Forms.Form
Outline
GroupBoxPanelExa
mple.vb
' Visual Studio.NET generated code
' event handlers to change lblMessage
Private Sub cmdHi_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdHi.Click
lblMessage.Text = "Hi pressed"
End Sub ' cmdHi_Click
' bye button handler
Private Sub cmdBye_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdBye.Click
Event handling code
displays the appropriate
text property for lbl.Message
lblMessage.Text = "Bye pressed"
End Sub ' cmdBye_Click
' far left button handler
Private Sub cmdLeft_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdLeft.Click
lblMessage.Text = "Far left pressed"
End Sub ' cmdLeft_Click
 2002 Prentice Hall.
All rights reserved.
33
30
31
32
33
34
35
36
37
' far right button handler
Private Sub cmdRight_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdRight.Click
lblMessage.Text = "Far right pressed"
End Sub ' cmdRight_Click
Outline
GroupBoxPanelExa
mple.vb
End Class ' FrmGroupBox
 2002 Prentice Hall.
All rights reserved.
34
12.7 CheckBoxes and RadioButtons
• State Buttons
– CheckBoxes
• Any number can be checked at a time
– RadioButtons
• Usually organized in groups and only one checked at a time
 2002 Prentice Hall. All rights reserved.
35
12.7 CheckBoxes and RadioButtons
CheckBox eve nts a nd
De sc rip tio n / De le g a te a nd Event Arg ume nts
p rop erties
Common
Properties
Checked
Indicates whether the CheckBox has been checked.
CheckState
Indicates whether the Checkbox is checked (contains a black
checkmark) or unchecked (blank). An enumeration with values
Checked, Unchecked or Indeterminate.
Text
Specifies the text displayed to the right of the CheckBox (called the
label).
Common Events
(Delegate EventHandler, event arguments
EventArgs)
CheckedChanged
Raised every time the Checkbox is either checked or unchecked.
When a user double-clicks the CheckBox control in design view, an
empty event handler for this event is generated.
CheckStateChanged Raised when the CheckState property changes.
Fig. 12.23
CheckBox p rop erties a nd events.
Fig. 12.23 CheckBox properties and events.
 2002 Prentice Hall. All rights reserved.
36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
' Fig. 12.24: CheckBoxTest.vb
' Using CheckBoxes to toggle italic and bold styles.
Public Class FrmCheckBox
Inherits System.Windows.Forms.Form
Outline
CheckBoxTest.vb
' Visual Studio .NET IDE generated code
' use Xor to toggle italic, keep other styles same
Private Sub chkItalic_CheckedChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles chkItalic.CheckedChanged
lblOutput.Font = New Font(lblOutput.Font.Name, _
lblOutput.Font.Size, lblOutput.Font.Style _
Xor FontStyle.Italic)
End Sub ' chkItalic_CheckedChanged
' use Xor to toggle bold, keep other styles same
Private Sub chkBold_CheckedChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles chkBold.CheckedChanged
lblOutput.Font = New Font(lblOutput.Font.Name, _
lblOutput.Font.Size, lblOutput.Font.Style _
Xor FontStyle.Bold)
End Sub ' chkBold_CheckedChanged
End Class ' FrmCheckBox
 2002 Prentice Hall.
All rights reserved.
37
Outline
CheckBoxTest.vb
 2002 Prentice Hall.
All rights reserved.
38
12.7 CheckBoxes and RadioButtons
RadioButton
De sc rip tio n / De le g a te a nd Event Arg ume nts
p rop erties a nd events
Common
Properties
Checked
Indicates whether the RadioButton is checked.
Text
Specifies the text displayed to the right of the RadioButton (called
the label).
Common Events
(Delegate EventHandler, event arguments
EventArgs)
Click
Raised when user clicks the control.
CheckedChanged
Raised every time the RadioButton is checked or unchecked.
When a user double-clicks the TextBox control in design view, an
empty event handler for this event is generated.
Fig. 12.25
RadioButton p rop e rties a nd events.
Fig. 12.25 RadioButton properties and events.
 2002 Prentice Hall. All rights reserved.
39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
' Fig. 12.26: RadioButtonTest.vb
' Using RadioButtons to set message window options.
Public Class FrmRadioButton
Inherits System.Windows.Forms.Form
Outline
RadioButtonTest.
vb
Private iconType As MessageBoxIcon
Private buttonType As MessageBoxButtons
' Visual Studio .NET generated code
' display message box and obtain dialogue button clicked
Private Sub cmdDisplay_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles cmdDisplay.Click
Dim dialog As DialogResult = MessageBox.Show( _
"This is Your Custom MessageBox", "VB", buttonType, _
iconType)
' check for dialog result and display on label
Select Case dialog
Case DialogResult.OK
lblDisplay.Text = "OK was pressed"
Case DialogResult.Cancel
lblDisplay.Text = "Cancel was pressed"
Case DialogResult.Abort
lblDisplay.Text = "Abort was pressed"
Case DialogResult.Retry
lblDisplay.Text = "Retry was pressed"
 2002 Prentice Hall.
All rights reserved.
40
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Case DialogResult.Ignore
lblDisplay.Text = "Ignore was pressed"
Case DialogResult.Yes
lblDisplay.Text = "Yes was pressed"
Outline
RadioButtonTest.
vb
Case DialogResult.No
lblDisplay.Text = "No was pressed"
End Select
End Sub ' cmdDisplay_Click
' set button type to OK
Private Sub radOk_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radOk.CheckedChanged
buttonType = MessageBoxButtons.OK
End Sub ' radOk_CheckedChanged
' set button type to OkCancel
Private Sub radOkCancel_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radOkCancel.CheckedChanged
buttonType = MessageBoxButtons.OKCancel
End Sub ' radOkCancel_CheckedChanged
' set button type to AbortRetryIgnore
Private Sub radAbortRetryIgnore_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radAbortRetryIgnore.CheckedChanged
buttonType = MessageBoxButtons.AbortRetryIgnore
End Sub ' radAbortRetryIgnore_CheckedChanged
 2002 Prentice Hall.
All rights reserved.
41
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
' set button type to YesNoCancel
Private Sub radYesNoCancel_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radYesNoCancel.CheckedChanged
Outline
RadioButtonTest.
vb
buttonType = MessageBoxButtons.YesNoCancel
End Sub ' radYesNoCancel_CheckedChanged
' set button type to YesNo
Private Sub radYesNo_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radYesNo.CheckedChanged
buttonType = MessageBoxButtons.YesNo
End Sub ' radYesNo_CheckedChanged
' set button type to RetryCancel
Private Sub radRetryCancel_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radRetryCancel.CheckedChanged
buttonType = MessageBoxButtons.RetryCancel
End Sub ' radRetryCancel_CheckedChanged
' set icon type to Asterisk when Asterisk checked
Private Sub radAsterisk_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radAsterisk.CheckedChanged
iconType = MessageBoxIcon.Asterisk
End Sub ' radAsterisk_CheckedChanged
 2002 Prentice Hall.
All rights reserved.
42
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
' set icon type to Error when Error checked
Private Sub radError_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radError.CheckedChanged
iconType = MessageBoxIcon.Error
End Sub ' radError_CheckedChanged
Outline
RadioButtonTest.
vb
' set icon type to Exclamation when Exclamation checked
Private Sub radExclamation_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radExclamation.CheckedChanged
iconType = MessageBoxIcon.Exclamation
End Sub ' radExclamation_CheckedChanged
' set icon type to Hand when Hand checked
Private Sub radHand_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radHand.CheckedChanged
iconType = MessageBoxIcon.Hand
End Sub ' radHand_CheckedChanged
' set icon type to Information when Information checked
Private Sub radInformation_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radInformation.CheckedChanged
iconType = MessageBoxIcon.Information
End Sub ' radInformation_CheckedChanged
 2002 Prentice Hall.
All rights reserved.
43
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
' set icon type to Question when Question checked
Private Sub radQuestion_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radQuestion.CheckedChanged
iconType = MessageBoxIcon.Question
End Sub ' radQuestion_CheckedChanged
Outline
RadioButtonTest.
vb
' set icon type to Stop when Stop checked
Private Sub radStop_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radStop.CheckedChanged
iconType = MessageBoxIcon.Stop
End Sub ' radStop_CheckedChanged
' set icon type to Warning when Warning checked
Private Sub radWarning_CheckedChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles radWarning.CheckedChanged
iconType = MessageBoxIcon.Warning
End Sub ' radWarning_CheckedChanged
End Class ' FrmRadioButtons
 2002 Prentice Hall.
All rights reserved.
44
Outline
RadioButtonTest.
vb
 2002 Prentice Hall.
All rights reserved.
45
Outline
RadioButtonTest.
vb
 2002 Prentice Hall.
All rights reserved.
46
Outline
RadioButtonTest.
vb
 2002 Prentice Hall.
All rights reserved.
47
Outline
RadioButtonTest.
vb
 2002 Prentice Hall.
All rights reserved.
48
12.8 PictureBoxes
• PictureBoxes
– Display images
•
•
•
•
Bitmap
GIF (Graphics Interchange Format)
JPEG (Joint Photographic Expert Group)
Metafile
– Image property
• Image to be displayed
 2002 Prentice Hall. All rights reserved.
49
12.8 PictureBoxes
PictureBox
De sc rip tio n / De le g a te a nd Event Arg ume nts
p rop erties a nd events
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 image in top-left corner of
PictureBox, and CenterImage puts image in middle (both cut
image off if it is too large). StretchImage resizes image to fit in
PictureBox. AutoSize resizes PictureBox to hold image.
Common Events
(Delegate EventHandler, event arguments
EventArgs)
Click
Raised when user clicks the control. Default event when this control is
double clicked in the designer.
Fig. 12.30 PictureBox p rop erties a nd e vents.
Fig. 12.30 PictureBox properties and events.
 2002 Prentice Hall. All rights reserved.
50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Outline
' Fig. 12.31: PictureBoxTest.vb
' Using a PictureBox to display images.
Imports System.IO
PictureBoxTest.v
b
Public Class FrmPictureBox
Inherits System.Windows.Forms.Form
Private imageNumber As Integer = -1
' Visual Studio.NET generated code
' replace image in picImage
Private Sub picImage_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles picImage.Click
' imageNumber from 0 to 2
imageNumber = (imageNumber + 1) Mod 3
An image object is created
from file and set as the
PictureBox’s image property
' create Image object from file, display in PictureBox
picImage.Image = Image.FromFile _
(Directory.GetCurrentDirectory & "\images\image" & _
imageNumber & ".bmp")
End Sub ' picImage_Click
End Class ' FrmPictureBox
 2002 Prentice Hall.
All rights reserved.
51
Outline
 2002 Prentice Hall.
All rights reserved.
52
12.9 Mouse-Event Handling
• MouseEventArgs Properties
– Button
• Mouse button pressed
– Clicks
• Number of mouse clicks
– X
• X-coordinate of event, within the control
– Y
• Y-coordinate of event, within the control
 2002 Prentice Hall. All rights reserved.
53
12.9 Mouse-Event Handling
Mouse Eve nts, Deleg a te s
a nd Event Arg um e nts
Mouse Events
(Delegate
EventHandler,
event arguments
EventArgs)
MouseEnter
Raised if the mouse cursor enters the area of the control.
MouseLeave
Raised if the mouse cursor leaves the area of the control.
Mouse Events
(Delegate
MouseEventHan
dler, event
arguments
MouseEventArg
s)
MouseDown
Raised if the mouse button is pressed while its cursor is over the area
of the control.
MouseHover
Raised if the mouse cursor hovers over the area of the control.
MouseMove
Raised if the mouse cursor is moved while in the area of the control.
MouseUp
Raised if the mouse button is released when the cursor is over the area
of the control.
Class
MouseEventArgs
Properties
Button
Specifies the mouse button that was pressed (left, right,
middle or none).
Clicks
Indicates the number of times that the mouse button was clicked.
X
The x-coordinate of the event, relative to the component.
Y
The y-coordinate of the event, relative to the component.
Fig. 12.32 Mouse events, d e leg a tes a nd e vent a rg um ents.
Fig. 12.32 Mouse events, delegates and event arguments.
 2002 Prentice Hall. All rights reserved.
54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Outline
' Fig. 12.33: Painter.vb
' Using the mouse to draw on a form.
Public Class FrmPainter
Inherits System.Windows.Forms.Form
Painter.vb
Dim shouldPaint As Boolean = False
' Visual Studio .NET IDE generated code
' draw circle if shouldPaint is True
Private Sub FrmPainter_MouseMove( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseMove
Event handling code begins
for event MouseMove
' paint circle if mouse pressed
If shouldPaint Then
Dim graphic As Graphics = CreateGraphics()
graphic.FillEllipse _
(New SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4)
End If
End Sub ' FrmPainter_MouseMove
' set shouldPaint to True
Private Sub FrmPainter_MouseDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseDown
Event handling for Mouse event
MouseDown sets variable
shouldPaint to True
_
shouldPaint = True
End Sub ' FrmPainter_MousDown
 2002 Prentice Hall.
All rights reserved.
55
35
36
37
38
39
40
41
42
43
' set shouldPaint to False
Private Sub FrmPainter_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseUp
Outline
Painter.vb
shouldPaint = False
End Sub ' FrmPainter_MouseUp
End Class ' FrmPainter
Event handling for Mouse event
MouseUp sets variable
shouldPaint to False
 2002 Prentice Hall.
All rights reserved.
56
12.10 Keyboard-Event Handling
• Key Events
– Generated when keys are pressed and released
– KeyPress
• Can return a Char for any ASCII character pressed
– KeyUp and KeyDown
• Test for special modifier keys
• Use KeyEventArgs
 2002 Prentice Hall. All rights reserved.
57
12.10 Keyboard-Event Handling
Keyb o a rd Eve nts, Deleg a te s a nd
Eve nt Arg ume nts
Key Events (Delegate
KeyEventHandler, event
arguments KeyEventArgs)
KeyDown
Raised when key is initially pushed down.
KeyUp
Raised when key is released.
Key Events (Delegate
KeyPressEventHandler,
event arguments
KeyPressEventArgs)
KeyPress
Raised when key is pressed. Occurs repeatedly while
key is held down, at a rate specified by the operating
system.
Class KeyPressEventArgs
Properties
KeyChar
Returns the ASCII character for the key pressed.
Handled
Indicates whether or not the KeyPress event was
handled.
Class KeyEventArgs
Properties
Alt
Indicates whether the Alt key was pressed.
Control
Indicates whether the Control key was pressed.
Shift
Indicates whether the Shift key was pressed.
Handled
Indicates whether the event was handled.
KeyCode
Returns the key code for the key as a Keys
enumeration. This does not include modifier-key
information. Used to test for a specific key.
KeyData
Returns the key code for a key as a Keys enumeration,
combined with modifier information. Used to
determine all information about the pressed key.
KeyValue
Returns the key code as an int, rather than as a Keys
enumeration. Used to obtain a numeric representation
of the pressed key.
Modifiers
Returns a Keys enumeration for any modifier keys
pressed (Alt, Control and Shift). Used to determine
modifier-key information only.
Fig. 12.34 Keyb oa rd eve nts, d e leg a tes a nd event a rg um ents.
Fig. 12.34 Keyboard events, delegates and event arguments.
 2002 Prentice Hall. All rights reserved.
58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Outline
' Fig. 12.35: KeyDemo.vb
' Displaying information about a user-pressed key.
Public Class FrmKeyDemo
Inherits System.Windows.Forms.Form
KeyDemo.vb
' Visual Studio.NET generated code
' event handler for key press
KeyPress event handler
Private Sub FrmKeyDemo_KeyPress(ByVal sender As System.Object, _
ByVal e As System.windows.Forms.KeyPressEventArgs) _ accesses the KeyChar property
of the KeyPressEventArgs object
Handles MyBase.KeyPress
lblCharacter.Text = "Key pressed: " & e.KeyChar
End Sub
and displays the Key
' display modifier keys, key code, key data and key value
Private Sub FrmKeyDemo_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyDown
lblInformation.Text = ""
' if key is Alt
If e.Alt Then
lblInformation.Text &= "Alt: Yes" & vbCrLf
Else
lblInformation.Text &= "Alt: No" & vbCrLf
End If
KeyDown event handler
displays information from
its KeyEventArgs object
 2002 Prentice Hall.
All rights reserved.
59
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Outline
' if key is Shift
If e.Shift Then
lblInformation.Text &= "Shift: Yes" & vbCrLf
Else
lblInformation.Text &= "Shift: No" & vbCrLfThe
End If
KeyDemo.vb
KeyCode property
returns
a Keys enumeration, which must be
converted to a String via method ToString
' if key is Ctrl
If e.Control Then
lblInformation.Text &= "Ctrl: Yes" & vbCrLf & _
"KeyCode: " & e.KeyCode.ToString & vbCrLf & _
"KeyData: " & e.KeyData.ToString & vbCrLf & _
"KeyValue: " & e.KeyValue
Else
lblInformation.Text &= "Ctrl: No" & vbCrLf & _
"KeyCode: " & e.KeyCode.ToString & vbCrLf & _
"KeyData: " & e.KeyData.ToString & vbCrLf & _
"KeyValue: " & e.KeyValue
End If
End Sub ' FrmKeyDemo_KeyDown
' clear labels when key is released
Private Sub FrmKeyDemo_KeyUp(ByVal sender As System.Object, _
ByVal e As System.windows.Forms.KeyEventArgs) _
Handles MyBase.KeyUp
Both labels
lblInformation.Text = ""
lblCharacter.Text = ""
End Sub ' FrmKeyDemo_KeyUp
are cleared
when a key is released
End Class ' FrmKeyDemo
 2002 Prentice Hall.
All rights reserved.
60
Outline
KeyDemo.vb
 2002 Prentice Hall.
All rights reserved.