T U T O R I A L Security Panel Application Introducing the Select Case Multiple-Selection Statement  2009 Pearson Education, Inc.

Download Report

Transcript T U T O R I A L Security Panel Application Introducing the Select Case Multiple-Selection Statement  2009 Pearson Education, Inc.

1
T U T O R I A L
12
Security Panel
Application
Introducing the Select Case
Multiple-Selection Statement
 2009 Pearson Education, Inc. All rights reserved.
2
Outline
12.1 Test-Driving the Security Panel
Application
12.2 Introducing the Select Case
Multiple-Selection Statement
12.3 Constructing the Security Panel
Application
 2009 Pearson Education, Inc. All rights reserved.
3
Objectives
In this tutorial you will learn:
■ Use the Select Case multiple-selection
statement.
■ Use Case statements.
■ Use the Is keyword.
■ Obtain the current date and time.
■ Display the date and time.
■ Use TextBox property PasswordChar.
 2009 Pearson Education, Inc. All rights reserved.
4
12.1 Test-Driving the Security Panel Application
Application Requirements
A lab wants to install a security panel outside a laboratory room.
Only authorized personnel may enter the lab, using their security
codes. The following are valid security codes and the groups of
employees they represent:
Values
Group
1645–1689
Technicians
8345
Custodians
9998, 1006–1008
Scientists
All access attempts are written to a window below the keypad. If access
is granted, the date, time and group are written to the window. If access
is denied, the date, the time and the message “Access Denied” are
written to the window. Furthermore, the user can enter any one-digit
access code to summon a security guard for assistance. The date, the
time and the message “Assistance Requested” are then written to the
window to indicate that the request has been received.
 2009 Pearson Education, Inc. All rights reserved.
5
Test-Driving the Security Panel Application
■ The TextBox displays an asterisk for each digit in the
security code entered using the GUI keypad (Fig. 12.1).
■ The C Button clears your current input, and the #
Button causes the application to process the security
code entered.
TextBox
Keypad
Output ListBox
Figure 12.1 | Security Panel application executing.
 2009 Pearson Education, Inc. All rights reserved.
6
GUI Design Tip
If your GUI is modeling a real-world object, its design
should mimic the physical appearance of the object.
 2009 Pearson Education, Inc. All rights reserved.
7
Test-Driving the Security Panel Application (Cont.)
■ Use the keypad to enter the invalid security code
1212 (Fig. 12.2).
An asterisk is displayed for each
numeric key pressed
Figure 12.2 | Asterisks displayed in Security code: field.
 2009 Pearson Education, Inc. All rights reserved.
8
Test-Driving the Security Panel Application (Cont.)
■ Submit by clicking the # Button (Fig. 12.3).
Message indicating that
an invalid security code
was entered
Figure 12.3 | Security Panel displaying Access Denied message.
 2009 Pearson Education, Inc. All rights reserved.
9
Test-Driving the Security Panel Application (Cont.)
■ Press a few numeric keys, then click the C Button.
■ Enter 1006, then click the #
Button (Fig. 12.4).
Message displayed
when a valid security
code is entered
Figure 12.4 | Security Panel application confirming a valid security-code entry.
 2009 Pearson Education, Inc. All rights reserved.
12.2 Introducing the Select Case
Multiple-Selection Statement
10
■ Look at an If...Then...Else multiple-selection
statement that displays a text message based on a
student’s grade:
If grade = "A" Then
displayLabel.Text = "Excellent!"
ElseIf grade = "B" Then
displayLabel.Text = "Very good!"
ElseIf grade = "C" Then
displayLabel.Text = "Good."
ElseIf grade = "D" Then
displayLabel.Text = "Poor."
ElseIf grade = "F" Then
displayLabel.Text = "Failure."
Else
displayLabel.Text = "Invalid grade."
End If
 2009 Pearson Education, Inc. All rights reserved.
12.2 Introducing the Select Case
Multiple-Selection Statement (Cont.)
11
■ The following Select Case multiple-selection
statement performs the same functionality as the
previous If...Then...Else statement:
Select Case grade
Case "A"
displayLabel.Text
Case "B"
displayLabel.Text
Case "C"
displayLabel.Text
Case "D"
displayLabel.Text
Case "F"
displayLabel.Text
Case Else
displayLabel.Text
End Select
= "Excellent!"
= "Very good!"
= "Good."
= "Poor."
= "Failure."
= "Invalid grade."
 2009 Pearson Education, Inc. All rights reserved.
12.2 Introducing the Select Case
Multiple-Selection Statement (Cont.)
12
■ The Select Case statement begins with the keywords
Select Case followed by a test expression and
terminates with keywords End Select.
■ The test expression is specified once.
■ The statement contains Case statements and the optional
Case Else statement.
– Each Case statement contains the keyword Case followed by an
expression list.
– The expression list can contain any built-in data type, such as
strings or numeric values.
– Each Case statement’s expression list is compared to the
Select Case statement’s test expression.
– Only one Case Else is allowed.
 2009 Pearson Education, Inc. All rights reserved.
13
Good Programming Practice
Visual Basic automatically indents the statements in
the body of each Case to improve readability.
 2009 Pearson Education, Inc. All rights reserved.
14
Common Programming Error
When using the optional Case Else statement in a
Select Case statement, failing to place the Case Else
as the last Case is a syntax error.
 2009 Pearson Education, Inc. All rights reserved.
12.2 Introducing the Select Case
Multiple-Selection Statement (Cont.)
15
■ Figure 12.5 shows the UML activity diagram for this
Select Case multiple-selection statement.
Figure 12.5 | Select Case multiple-selection statement UML activity diagram.
 2009 Pearson Education, Inc. All rights reserved.
16
Common Programming Error
Case statements that have the same value in their
expression lists result in logic errors. At runtime,
only the body of the first matching Case executes.
 2009 Pearson Education, Inc. All rights reserved.
17
12.3 Constructing the Security Panel Application
■ The following pseudocode describes the Click event
handler for each numeric Button:
If a numeric Button is clicked
Concatenate Button’s digit to the TextBox’s Text
property value
■ The pseudocode for the # Button’s event handler is
as follows:
 2009 Pearson Education, Inc. All rights reserved.
12.3 Constructing the Security
Panel Application (Cont.)
18
When the user clicks the # Button
Retrieve security code input by user
Clear input TextBox
Select correct Case based on access code
Case where access code is less than 10
Store text “Assistance Requested” in a String variable
Case where access code is in the range 1645 to 1689
Store text “Technicians” in a String variable
Case where access code equals 8345
Store text “Custodians” in a String variable
Case where access code equals 9998 or is in the range 1006 to 1008
Store text “Scientists” in a String variable
Case where none of the preceding Cases match
Store text “Access Denied” in a String variable
Insert a message containing the current time and the String variable’s
contents in the ListBox
 2009 Pearson Education, Inc. All rights reserved.
Action/Control/Event (ACE) Table for
the Security Panel Application
19
■ Use an ACE table to convert pseudocode to
Visual Basic (Fig. 12.6).
Action
Control
Label the application’s fields
securityCodeLabel,
accessLogLabel
oneButton,
twoButton,
threeButton,
fourButton,
fiveButton,
sixButton,
sevenButton,
eightButton,
nineButton,
zeroButton
Concatenate Button’s digit to the TextBox’s
Text property value
Event
Click
securityCodeTextBox
Figure 12.6 | ACE table for Security Panel application. (Part 1 of 3.)
 2009 Pearson Education, Inc. All rights reserved.
Action/Control/Event (ACE) Table for the
Security Panel Application (Cont.)
Action
Clear input TextBox
Control
Event
clearButton
Click
20
securityCodeTextBox
enterButton
Retrieve security code input by user
securityCodeTextBox
Clear input TextBox
securityCodeTextBox
Click
Select correct Case based on access code
Case where access code is less than 10
Store text “Assistance Requested”
Figure 12.6 | ACE table for Security Panel application. (Part 1 of 3.)
 2009 Pearson Education, Inc. All rights reserved.
Action/Control/Event (ACE) Table for the
Security Panel Application (Cont.)
Action
Control
21
Event
Case where access code is in the range
1645 to 1689
Store text “Technicians”
Case where access code equals 8345
Store text “Custodians”
Case where access code equals 9998 or is
in the range 1006 to 1008
Store text “Scientists”
Case where none of preceding Cases match
Store text “Access Denied”
Insert a message containing the current
time and the String variable’s contents
in the ListBox
logEntryListBox
Figure 12.6 | ACE table for Security Panel application. (Part 1 of 3.)
 2009 Pearson Education, Inc. All rights reserved.
22
Using the PasswordChar Property
■ Set the Security code: TextBox’s
PasswordChar property to * in the Properties
window
– Text displayed in a TextBox can be masked with the
character specified in property PasswordChar.
– Masking characters are displayed rather than the
actual text that the user types.
– However, the TextBox’s Text property does contain the
text the user typed.
■ To prevent users from directly modifying the text in the
TextBox, set its Enabled property to False.
 2009 Pearson Education, Inc. All rights reserved.
23
GUI Design Tip
Mask passwords or other sensitive pieces of
information in TextBoxes.
 2009 Pearson Education, Inc. All rights reserved.
24
Using the PasswordChar Property (Cont.)
■ Double click the # Button to create the
enterButton_Click event handler (Fig. 12.7).
Declaring event
handler’s variables
Figure 12.7 | Variable declarations for enterButton_Click.
 2009 Pearson Education, Inc. All rights reserved.
25
Adding a Select Case Statement to the Application
■ The test expression accessCode is the code entered
by the user (Fig. 12.8).
Creating a Select
Case statement
Figure 12.8 | Select Case statement.
 2009 Pearson Education, Inc. All rights reserved.
Adding a Select Case Statement
to the Application (Cont.)
26
■ Keyword Is followed by a relational or equality
operator can be used to compare the controlling
expression and the value to the right of the operator.
– If the value in accessCode is less than 10, the code in
the body of the Case statement executes and message is
assigned the text "Assistance Requested" (Fig. 12.9).
Is keyword can be
used for relational and
equality comparisons
Figure 12.9 | First Case added to Select Case statement.
 2009 Pearson Education, Inc. All rights reserved.
Adding a Select Case Statement
to the Application (Cont.)
27
■ Keyword To is used to specify a range of values.
■ Note that when multiple values or value ranges are
provided in a Case statement, they are separated by
commas (Fig. 12.10).
To keyword can be used to specify
a range of values to test
Comma used to separate multiple
expressions in a Case
Figure 12.10 | Cases specified for remaining access codes
 2009 Pearson Education, Inc. All rights reserved.
28
Common Programming Error
If the value on the left side of the To keyword in a Case
statement is larger than the value on the right side, the
Case is ignored during application execution,
potentially causing a logic error. The compiler issues a
warning in this case.
 2009 Pearson Education, Inc. All rights reserved.
Adding a Select Case Statement
to the Application (Cont.)
29
■ The optional Case Else is executed when the
controlling expression does not match any of the
previous Cases (Fig. 12.11).
– The Case Else must follow all other Case statements.
■ The keywords End Select terminate the Select
Case statement.
Case Else statement
executes when no
other Case matches
Figure 12.11 | Case Else of the Select Case statement.
 2009 Pearson Education, Inc. All rights reserved.
Adding a Select Case Statement
to the Application (Cont.)
30
■ The Items property’s Insert method inserts an item
into the ListBox at a specified position.
■ The first part of method Insert’s second argument
contains the expression Date.Now (Fig. 12.12).
– The .NET Framework Class Library uses a Date type that
can be used to store and display date and time information.
Figure 12.12 | Updating the Security Panel application’s ListBox.
 2009 Pearson Education, Inc. All rights reserved.
31
Programming the Remaining Event Handlers
■ Double click the 0 Button (zeroButton) to create
the zeroButton_Click event handler (Fig. 12.13).
Figure 12.13 | Event handler zeroButton_Click.
 2009 Pearson Education, Inc. All rights reserved.
32
Programming the Remaining Event Handlers (Cont.)
■ Repeat for the remaining numeric Buttons
(1 through 9) (Fig. 12.14).
Figure 12.14 | Event handlers oneButton_Click and twoButton_Click.
 2009 Pearson Education, Inc. All rights reserved.
33
Programming the Remaining Event Handlers (Cont.)
■ Double click the C Button, and have its event handler
clear the Security code: TextBox (Fig. 12.15).
Figure 12.15 | Event handler clearButton_Click defined.
 2009 Pearson Education, Inc. All rights reserved.
34
Outline
■ Figure 12.16 presents the source code
for the application.
1
2
3
4
(1 of 5 )
Public Class SecurityPanelForm
' handles enterButton's Click event
Private Sub enterButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles enterButton.Click
5
6
7
8
Dim accessCode As Integer ' stores access code entered
Dim message As String ' displays access status of users
9
accessCode = Val(securityCodeTextBox.Text)
securityCodeTextBox.Clear()
10
11
12
13
14
15
16
Select Case accessCode ' check access code input
Case Is < 10 ' access code less than 10
Using a Select Case statement to
determine user access level
message = "Assistance Requested"
Case 1645 To 1689 ' access code between 1645 and 1689
message = "Technicians"
18
Case 8345 ' access code equal to 8345
message = "Custodians"
19
20
Case 9998, 1006 To 1008 ' 9998 or between 1006 and 1008
message = "Scientists"
17
 2009 Pearson Education,
Inc. All rights reserved.
35
Outline
(2 of 5 )
21
22
23
Case Else ' if no other Case is True
message = "Access Denied"
End Select
24
25
' display time and message in ListBox
logEntryListBox.Items.Insert(0, Date.Now & "
" & message)
26
27
28
End Sub ' enterButton_Click
29
30
31
' handles zeroButton's Click event
Private Sub zeroButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles zeroButton.Click
Obtain the current date
and time using
Date.Now
32
33
34
securityCodeTextBox.Text &= "0" ' concatenate "0" to display
End Sub ' zeroButton_Click
35
36
' handles oneButton's Click event
37
38
Private Sub oneButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles oneButton.Click
39
40
41
Appending text
to a disabled
TextBox for
output purposes
securityCodeTextBox.Text &= "1" ' concatenate "1" to display
End Sub ' oneButton_Click
 2009 Pearson Education,
Inc. All rights reserved.
36
Outline
(3 of 5 )
42
43
' handles twoButton's Click event
44
45
46
47
Private Sub twoButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles twoButton.Click
48
49
End Sub ' twoButton_Click
50
51
' handles threeButton's Click event
Private Sub threeButton_Click(ByVal sender As System.Object, _
securityCodeTextBox.Text &= "2" ' concatenate "2" to display
ByVal e As System.EventArgs) Handles threeButton.Click
52
53
54
55
securityCodeTextBox.Text &= "3" ' concatenate "3" to display
End Sub ' threeButton_Click
56
57
' handles fourButton's Click event
58
59
Private Sub fourButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles fourButton.Click
60
61
62
securityCodeTextBox.Text &= "4" ' concatenate "4" to display
End Sub ' fourButton_Click
 2009 Pearson Education,
Inc. All rights reserved.
37
Outline
(4 of 5 )
63
64
' handles fiveButton's Click event
65
Private Sub fiveButton_Click(ByVal sender As System.Object, _
66
67
ByVal e As System.EventArgs) Handles fiveButton.Click
68
securityCodeTextBox.Text &= "5" ' concatenate "5" to display
69
70
End Sub ' fiveButton_Click
71
' handles sixButton's Click event
72
73
74
Private Sub sixButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles sixButton.Click
75
securityCodeTextBox.Text &= "6" ' concatenate "6" to display
76
77
78
End Sub ' sixButton_Click
79
Private Sub sevenButton_Click(ByVal sender As System.Object, _
80
81
82
83
84
' handles sevenButton's Click event
ByVal e As System.EventArgs) Handles sevenButton.Click
securityCodeTextBox.Text &= "7" ' concatenate "7" to display
End Sub ' sevenButton_Click
 2009 Pearson Education,
Inc. All rights reserved.
38
Outline
(5 of 5 )
85
' handles eightButton's Click event
86
Private Sub eightButton_Click(ByVal sender As System.Object, _
87
88
89
ByVal e As System.EventArgs) Handles eightButton.Click
securityCodeTextBox.Text &= "8" ' concatenate "8" to display
90
End Sub ' eightButton_Click
91
92
' handles nineButton's Click event
93
94
Private Sub nineButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles nineButton.Click
95
96
97
98
securityCodeTextBox.Text &= "9" ' concatenate "9" to display
End Sub ' nineButton_Click
99
100
' handles clearButton's Click event
Private Sub clearButton_Click(ByVal sender As System.Object, _
101
102
ByVal e As System.EventArgs) Handles clearButton.Click
103
securityCodeTextBox.Clear() ' clear text from TextBox
104
End Sub ' clearButton_Click
105 End Class ' SecurityPanelForm
 2009 Pearson Education,
Inc. All rights reserved.