© copyright 1992-2004 by deitel & associates, inc. and pearson education inc. all rights...
TRANSCRIPT
1
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Tutorial 12 – Security Panel Application
Introducing the Select Case Multiple-Selection Statement
Outline
12.1 Test-Driving the Security Panel Application12.2 Introducing the Select Case Multiple-Selection Statement12.3 Constructing the Security Panel Application12.4 Wrap-Up
2
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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.
3
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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.
4
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.1 Test-Driving the Security Panel Application
• Load the Wage Calculator application– Debug > Start
5
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.1 Test-Driving the Security Panel Application
Figure 12.1 Security Panel application executing.
TextBox
Output ListBox
Keypad
6
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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
7
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.1 Test-Driving the Security Panel Application
• Entering invalid code– Enter 1212
– Click # Button
8
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.1 Test-Driving the Security Panel Application
Figure 12.3 Security Panel displaying Access Denied message.
Message indicating that an invalid security code was entered
9
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.1 Test-Driving the Security Panel Application
• Entering valid code– Enter 1006– Click # Button
10
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.1 Test-Driving the Security Panel Application
Figure 12.4 Security Panel application confirming a valid security-code entry.
Message displayed when a valid security code is entered
11
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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
12
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.2 Introducing the Select Case Multiple-Selection Statement
display "Excellent!"
[strGrade = "A"]
[strGrade <> "A"]
[strGrade = "B"]
[strGrade = "F"]
.
.
Case "A"
case b
Case "F"
C ase "B" display "Very good!"
display "Failure."
display "Inva lid grade."
[strGrade <> "B"]
[strGrade <> "F"]
Figure 12.5 Select Case multiple-selection statement UML activity diagram.
13
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Action Control Event Label the application’s fields lblSecurityCode,
lblAccessLog
btnEnter Click
Retrieve security code input by user txtSecurityCode
Clear input TextBox txtSecurityCode
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.
14
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Action Control Event 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
lstLogEntry
Figure 12.6 ACE table for Security Panel application.
15
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Figure 12.7 Variable declarations for btnEnter_Click.
Declaring event handler’s variables
• Declaring variables
• Clearing the TextBox
16
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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, <)
17
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Figure 12.8 Select Case statement.
Creating a Select Case
statement
• Create Select Case statement
– Set controlling expression
18
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Figure 12.9 First Case added to Select Case statement.
Is keyword can be used for relational and equality comparisons
19
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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
20
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Figure 12.10 Cases specified for remaining access codes.
To keyword can be used to specify a range of
values to test.
Comma used to separate multiple expressions in a Case
21
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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
22
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Figure 12.11 Case Else of the Select Case statement.
Case Else statement executes when no other
Case matches
23
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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
24
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Figure 12.12 Updating the Security Panel application’s ListBox.
25
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Figure 12.13 Event handler btnZero_Click.
• Appending “0” to the end of a String
26
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Figure 12.14 Event handlers btnOne_Click and btnTwo_Click.
• Appending “1” and “2” to the end of a String
27
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12.3 Constructing the Security Panel Application
Figure 12.15 Event handler btnClear_Click defined.
• Clearing the TextBox
Outline28
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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, _
7 ByVal e As System.EventArgs) Handles btnEnter.Click
8
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()
14
15 Select Case intAccessCode ' check access code input
16
17 ' access code less than 10
18 Case Is < 10
19 strMessage = "Restricted Access"
20
21 ' access code between 1645 and 1689
22 Case 1645 To 1689
23 strMessage = "Technicians"
24
SecurityPanel.vb(1 of 5)
Retrieving access code and clearing TextBox
Declaring variables
Using a Select Case statement to determine user access level
Outline29
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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
SecurityPanel.vb(2 of 5)
Appending the numeric Button value to the text stored in the TextBox
Outline30
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education 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
SecurityPanel.vb(3 of 5)
Outline31
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
SecurityPanel.vb(4 of 5)
Outline32
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
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
SecurityPanel.vb(5 of 5)
Clearing the TextBox