Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 12 Security Panel Application Introducing the Select Case Multiple-Selection Statement."— Presentation transcript:

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

2  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

3  2009 Pearson Education, Inc. All rights reserved. 3 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. Objectives

4 Application Requirements  2009 Pearson Education, Inc. All rights reserved. 4 12.1 Test-Driving the Security Panel Application 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: ValuesGroup 1645–1689Technicians 8345Custodians 9998, 1006–1008Scientists 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.

5  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. Figure 12.1 | Security Panel application executing. Output ListBox Keypad TextBox

6  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.

7  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). Figure 12.2 | Asterisks displayed in Security code: field. An asterisk is displayed for each numeric key pressed

8  2009 Pearson Education, Inc. All rights reserved. 8 Test-Driving the Security Panel Application (Cont.) ■Submit by clicking the # Button (Fig. 12.3). Figure 12.3 | Security Panel displaying Access Denied message. Message indicating that an invalid security code was entered

9  2009 Pearson Education, Inc. All rights reserved. 9 ■Press a few numeric keys, then click the C Button. ■ Enter 1006, then click the # Button (Fig. 12.4). Test-Driving the Security Panel Application (Cont.) Figure 12.4 | Security Panel application confirming a valid security-code entry. Message displayed when a valid security code is entered

10  2009 Pearson Education, Inc. All rights reserved. 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 12.2 Introducing the Select Case Multiple-Selection Statement

11  2009 Pearson Education, Inc. All rights reserved. 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 = "Excellent!" Case "B" displayLabel.Text = "Very good!" Case "C" displayLabel.Text = "Good." Case "D" displayLabel.Text = "Poor." Case "F" displayLabel.Text = "Failure." Case Else displayLabel.Text = "Invalid grade." End Select 12.2 Introducing the Select Case Multiple-Selection Statement (Cont.)

12  2009 Pearson Education, Inc. All rights reserved. 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. 12.2 Introducing the Select Case Multiple-Selection Statement (Cont.)

13  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.

14  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.

15  2009 Pearson Education, Inc. All rights reserved. 15 Figure 12.5 | Select Case multiple-selection statement UML activity diagram. 12.2 Introducing the Select Case Multiple-Selection Statement (Cont.) ■Figure 12.5 shows the UML activity diagram for this Select Case multiple-selection statement.

16  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.

17  2009 Pearson Education, Inc. All rights reserved. 17 ■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: 12.3 Constructing the Security Panel Application

18  2009 Pearson Education, Inc. All rights reserved. 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 12.3 Constructing the Security Panel Application (Cont.)

19  2009 Pearson Education, Inc. All rights reserved. 19 ■Use an ACE table to convert pseudocode to Visual Basic (Fig. 12.6). Figure 12.6 | ACE table for Security Panel application. (Part 1 of 3.) Action/Control/Event (ACE) Table for the Security Panel Application

20  2009 Pearson Education, Inc. All rights reserved. 20 Action/Control/Event (ACE) Table for the Security Panel Application (Cont.) Figure 12.6 | ACE table for Security Panel application. (Part 1 of 3.)

21  2009 Pearson Education, Inc. All rights reserved. 21 Action/Control/Event (ACE) Table for the Security Panel Application (Cont.) Figure 12.6 | ACE table for Security Panel application. (Part 1 of 3.)

22  2009 Pearson Education, Inc. All rights reserved. 22 ■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. Using the PasswordChar Property

23  2009 Pearson Education, Inc. All rights reserved. 23 GUI Design Tip Mask passwords or other sensitive pieces of information in TextBox es.

24  2009 Pearson Education, Inc. All rights reserved. 24 ■Double click the # Button to create the enterButton_Click event handler (Fig. 12.7). Figure 12.7 | Variable declarations for enterButton_Click. Declaring event handler’s variables Using the PasswordChar Property (Cont.)

25  2009 Pearson Education, Inc. All rights reserved. 25 ■The test expression accessCode is the code entered by the user (Fig. 12.8). Figure 12.8 | Select Case statement. Creating a Select Case statement Adding a Select Case Statement to the Application

26  2009 Pearson Education, Inc. All rights reserved. 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). Adding a Select Case Statement to the Application (Cont.) Figure 12.9 | First Case added to Select Case statement. Is keyword can be used for relational and equality comparisons

27  2009 Pearson Education, Inc. All rights reserved. 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). Figure 12.10 | Case s specified for remaining access codes To keyword can be used to specify a range of values to test Adding a Select Case Statement to the Application (Cont.) Comma used to separate multiple expressions in a Case

28  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.

29  2009 Pearson Education, Inc. All rights reserved. 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. Adding a Select Case Statement to the Application (Cont.) Figure 12.11 | Case Else of the Select Case statement. Case Else statement executes when no other Case matches

30  2009 Pearson Education, Inc. All rights reserved. 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. Adding a Select Case Statement to the Application (Cont.) Figure 12.12 | Updating the Security Panel application’s ListBox.

31  2009 Pearson Education, Inc. All rights reserved. 31 ■Double click the 0 Button ( zeroButton ) to create the zeroButton_Click event handler (Fig. 12.13). Figure 12.13 | Event handler zeroButton_Click. Programming the Remaining Event Handlers

32  2009 Pearson Education, Inc. All rights reserved. 32 ■Repeat for the remaining numeric Button s ( 1 through 9 ) (Fig. 12.14). Figure 12.14 | Event handlers oneButton_Click and twoButton_Click. Programming the Remaining Event Handlers (Cont.)

33  2009 Pearson Education, Inc. All rights reserved. 33 ■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. Programming the Remaining Event Handlers (Cont.)

34  2009 Pearson Education, Inc. All rights reserved. 34 ■Figure 12.16 presents the source code for the application. Outline (1 of 5 ) Using a Select Case statement to determine user access level

35  2009 Pearson Education, Inc. All rights reserved. 35 Outline (2 of 5 ) Obtain the current date and time using Date.Now Appending text to a disabled TextBox for output purposes

36  2009 Pearson Education, Inc. All rights reserved. 36 Outline (3 of 5 )

37  2009 Pearson Education, Inc. All rights reserved. 37 Outline (4 of 5 )

38  2009 Pearson Education, Inc. All rights reserved. 38 Outline (5 of 5 )


Download ppt "T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 12 Security Panel Application Introducing the Select Case Multiple-Selection Statement."

Similar presentations


Ads by Google