Transcript Tutorial 12 – Security Panel Application Introducing the
Tutorial 12 –
Security Panel
Application
Introducing the
Select Case
Multiple-Selection Statement
Outline 12.1 12.2 12.3 12.4 Test-Driving the
Security Panel
Introducing the Select Case Application Multiple-Selection Statement Constructing the
Security Panel
Application Wrap-Up 1 2003 Prentice Hall, Inc. All rights reserved.
Objectives • In this tutorial, you will learn to:
– Use the Select Case multiple-selection statement.
– Use Case statements.
– Use the Is keyword.
– Display a date and time.
– Use TextBox property PasswordChar .
2 2003 Prentice Hall, Inc. All rights reserved.
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 (also called access codes) and the groups of employees they represent: Values Groups 1645–1689 Technicians 8345 Custodians 9998, 1006–1008 Scientists Once a security code is entered, access is either granted or denied. All access attempts are written to a window below the keypad. If access is granted, the date, time and group (scientists, custodians, etc.) are written to the window. If access is denied, the date, the time and a 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 a message, “Restricted Access,” are then written to the window to indicate that the request has been received.
3 2003 Prentice Hall, Inc. All rights reserved.
12.1 Test-Driving the
Security Panel
Application
• Load the
Wage Calculator
application
–
Debug > Start
4 2003 Prentice Hall, Inc. All rights reserved.
12.1 Test-Driving the
Security Panel
Application Figure 12.1
Security Panel
application executing. TextBox Keypad Output ListBox 5 2003 Prentice Hall, Inc. All rights reserved.
12.1 Test-Driving the
Security Panel
Application Figure 12.2 Asterisks displayed in
Security code:
field. An asterisk is displayed for each numeric key pressed 6 2003 Prentice Hall, Inc. All rights reserved.
12.1 Test-Driving the
Security Panel
Application
• Entering invalid code
– Enter 1212 – Click
#
Button 7 2003 Prentice Hall, Inc. All rights reserved.
12.1 Test-Driving the
Security Panel
Application Figure 12.3
Security Panel
displaying
Access Denied
message. 8 Message indicating that an invalid security code was entered 2003 Prentice Hall, Inc. All rights reserved.
12.1 Test-Driving the
Security Panel
Application •
Entering valid code
– Enter 1006 – Click
#
Button 9 2003 Prentice Hall, Inc. All rights reserved.
12.1 Test-Driving the
Security Panel
Application Figure 12.4
Security Panel
application confirming a valid security-code entry .
10 Message displayed when a valid security code is entered 2003 Prentice Hall, Inc. All rights reserved.
12.2 Introducing the Select Case Multiple Selection Statement • Select Case
statement
– Begins with keywords Select Case followed by test expression – Can contain optional Case Else statement – Terminates with keywords End Select 11 2003 Prentice Hall, Inc. All rights reserved.
12.2 Introducing the Select Case Multiple Selection Statement Figure 12.5
Select Case multiple-selection statement UML activity diagram.
Ca se "A" [ strGrade
<>
"A" ] [ strGrade
=
"A" ] d isp la y "Exc ellent! " case b [ strGrade
<>
"B" ] .
.
[ strGrade
=
"B" ] d isp la y "Very good !" Ca se "F" [ strGrade
<>
"F" ] [ strGrade
=
"F" ] d isp la y "Failure." display "Inva lid grad e." 12 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Action
Label the application’s fields 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 “Restricted Access” Case where access code is in the range 1645 to 1689 Store text “Technicians”
Figure 12.6
ACE table for Security Panel application.
Control lblSecurityCode , lblAccessLog btnEnter txtSecurityCode txtSecurityCode Event Click 13 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Action
Case where access code equals 8345 Store text “Scientists” 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” Display message in ListBox with current time and String variable’s contents
Figure 12.6
ACE table for Security Panel application.
Control lstLogEntry Event 14 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Figure 12.7 Variable declarations for btnEnter_Click .
15 Declaring event handler’s variables • Declaring variables • Clearing the TextBox 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application
• Creating
Case
statement
– Specify a range of values using: • Keyword Is • Comparison operator (in this case, < ) 16 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Figure 12.8
Select Case statement. 17 Creating a Select Case statement • Create Select Case statement – Set controlling expression 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Figure 12.9 First Case added to Select Case statement. 18 Is keyword can be used for relational and equality comparisons 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application
• Creating a
Case
statement
– Specifying a range of values using: • Keyword To – Checking for a specific number – Specifying multiple expressions • Use a comma to separate expressions 19 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Figure 12.10
Cases specified for remaining access codes. 20 To keyword can be used to specify a range of values to test.
Comma used to separate multiple expressions in a Case 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application
• Creating a
Case Else
statement
– Use keywords Case Else – Must follow all other Case statements 21 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Figure 12.11
Case Else of the Select Case statement. 22 Case Else statement executes when no other case matches 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application • Date
structure
– Stores and displays date and time information – Property Now returns: • System time as a Date 23 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Figure 12.12 Updating the
Security Panel
application’s ListBox .
24 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Figure 12.13 Event handler btnZero_Click . 25 • Appending “ 0 ” to the end of a String 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Figure 12.14 Event handlers btnOne_Click and btnTwo_Click . 26 • Appending “ 1 ” and “ 2 ” to the end of a String 2003 Prentice Hall, Inc. All rights reserved.
12.3 Constructing the
Security Panel
Application Figure 12.15 Event handler btnClear_Click defined. 27 • Clearing the TextBox 2003 Prentice Hall, Inc. All rights reserved.
1
Public Class FrmSecurityPanel
2
Inherits System.Windows.Forms.Form
3 4
' Windows Form Designer generated code
5 6
Private Sub btnEnter_Click( ByVal sender As System.Object, _
(1 of 5)
Outline
SecurityPanel.vb
7
ByVal e As System.EventArgs) Handles btnEnter.Click
8 14 15
Select Case intAccessCode ' check access code input
16 17
' access code less than 10 Declaring variables
9
Dim intAccessCode As Integer ' stores access code entered
10
Dim strMessage As String ' displays access status of users
11 12
intAccessCode = Val(txtSecurityCode.Text)
13
txtSecurityCode.Clear() Retrieving access code and clearing TextBox
18
Case Is < 10
19
strMessage = "Restricted Access" Using a Select Case statement to determine user access level
20 21
' access code between 1645 and 1689
22
Case 1645 To 1689
23
strMessage = "Technicians"
24
28 2003 Prentice Hall, Inc. All rights reserved.
2001 Prentice Hall, Inc.
All rights reserved.
25
' access code equal to 8345
26
Case 8345
27
strMessage = "Custodians"
28 29
' access code equal to 9998 or between
30
' 1006 and 1008, inclusive
31
Case 9998 , 1006 To 1008
32
strMessage = "Scientists"
33 34
' if no other Case is True
35
Case Else
36
strMessage = "Access Denied"
37 38
End Select
39 40
' display time and message in ListBox
41
lstLogEntry.Items.Add( Date .Now & " " & strMessage)
42
End Sub ' btnEnter_Click
43 44
Private Sub btnZero_Click( ByVal sender As System.Object, _
45
ByVal e As System.EventArgs) Handles btnZero.Click
46 47
txtSecurityCode.Text &= "0" ' concatenate "0" to display
48
End Sub ' btnZero_Click
49 (2 of 5)
Appending the numeric Button value to the text stored in the TextBox Outline
SecurityPanel.vb
29 2003 Prentice Hall, Inc. All rights reserved.
2001 Prentice Hall, Inc.
All rights reserved.
50
Private Sub btnOne_Click( ByVal sender As System.Object, _
51
ByVal e As System.EventArgs) Handles btnOne.Click
52 53
txtSecurityCode.Text &= "1" ' concatenate "1" to display
54
End Sub ' btnOne_Click
55 56
Private Sub btnTwo_Click( ByVal sender As System.Object, _
57
ByVal e As System.EventArgs) Handles btnTwo.Click
58 59
txtSecurityCode.Text &= "2" ' concatenate "2" to display
60
End Sub ' btnTwo_Click
61 62
Private Sub btnThree_Click( ByVal sender As System.Object, _
63
ByVal e As System.EventArgs) Handles btnThree.Click
64 65
txtSecurityCode.Text &= "3" ' concatenate "3" to display
66
End Sub ' btnThree_Click
67 68
Private Sub btnFour_Click( ByVal sender As System.Object, _
69
ByVal e As System.EventArgs) Handles btnFour.Click
70 71
txtSecurityCode.Text &= "4" ' concatenate "4" to display
72
End Sub ' btnFour_Click
73
2003 Prentice Hall, Inc. All rights reserved.
Outline
SecurityPanel.vb
(3 of 5)
2001 Prentice Hall, Inc.
All rights reserved.
30
74
Private Sub btnFive_Click( ByVal sender As System.Object, _
75
ByVal e As System.EventArgs) Handles btnFive.Click
76 77
txtSecurityCode.Text &= "5" ' concatenate "5" to display
78
End Sub ' btnFive_Click
79 80
Private Sub btnSix_Click( ByVal sender As System.Object, _
81
ByVal e As System.EventArgs) Handles btnSix.Click
82 83
txtSecurityCode.Text &= "6" ' concatenate "6" to display
84
End Sub ' btnSix_Click
85 86
Private Sub btnSeven_Click( ByVal sender As System.Object, _
87
ByVal e As System.EventArgs) Handles btnSeven.Click
88 89
txtSecurityCode.Text &= "7" ' concatenate "7" to display
90
End Sub ' btnSeven_Click
91 92
Private Sub btnEight_Click( ByVal sender As System.Object, _
93
ByVal e As System.EventArgs) Handles btnEight.Click
94 95
txtSecurityCode.Text &= "8" ' concatenate "8" to display
96
End Sub ' btnEight_Click
97
2003 Prentice Hall, Inc. All rights reserved.
Outline
SecurityPanel.vb
(4 of 5)
2001 Prentice Hall, Inc.
All rights reserved.
31
98
Private Sub btnNine_Click( ByVal sender As System.Object, _
99
ByVal e As System.EventArgs) Handles btnNine.Click
100 101
txtSecurityCode.Text &= "9" ' concatenate "9" to display
102
End Sub ' btnNine_Click
103 104
Private Sub btnClear_Click( ByVal sender As System.Object, _
105
ByVal e As System.EventArgs) Handles btnClear.Click
106 107
txtSecurityCode.Clear() ' clear text from TextBox
108
End Sub ' btnClear_Click
109 110
End Class ' FrmSecurityPanel
(5 of 5)
Clearing the TextBox Outline
SecurityPanel.vb
32 2003 Prentice Hall, Inc. All rights reserved.
2001 Prentice Hall, Inc.
All rights reserved.