Microsoft Visual Basic Reloaded Chapter Five More on
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure
Overview ■ ■ ■ ■ The Dental Payment Application Using Check. Boxes Message Dialog Windows Nested selection structure Multiple-alternative selection structure Select Case statement Include radio buttons in an interface Prevent the entry of invalid characters in a text box 2009 Pearson Education, Inc. All rights reserved.
Dental Payment Application ■ A Check. Box is a small square that either is blank or contains a check mark ( ). Check. Box controls (unchecked) 2009 Pearson Education, Inc. All rights reserved.
Designing the Dental Payment Application When the user clicks the Calculate Button Clear previous output If user has not entered a patient name or has not selected any Check. Boxes Display message in dialog Else Initialize the total to zero If “Cleaning” Check. Box is selected Add cost of a cleaning to the total If “Cavity Filling” Check. Box is selected Add cost of receiving a cavity filling to the total If “X-Ray” Check. Box is selected Add cost of receiving an x-ray to the total Format total to be displayed as currency Display total 2009 Pearson Education, Inc. All rights reserved.
Using Check. Boxes ■ A Check. Box is known as a state button because it can be in the on/off [true/false] state. ■ The text that appears alongside a Check. Box is called the Check. Box label. ■ If the Check. Box is checked, the Checked property contains the Boolean value True; otherwise, it contains False. 2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
Using a Dialog to Display a Message ■ This dialog contains: a title bar a close box a message an OK button an icon indicating the tone of the message Title bar Icon indicates the tone of the message Close box Dialog sized to accommodate contents OK Button allows the user to close the dialog 2009 Pearson Education, Inc. All rights reserved.
Using a Dialog to Display a Message Figure 8. 11 | Message dialog code that displays a message to users. 2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
Applying these concepts ■ Revise the code so that users must enter a name and select at least one Check. Box before they click the Calculate Button. If they don’t an error dialog will be displayed. ■ Use If Then Else, Or. Else and And. Also to determine if name is entered and at least one Check. Box is selected. ■ clean. Check. Box ■ xray. Check. Box ■ cavity. Check. Box ■ name. Text. Box 2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
Making More Than One Decision ■ Nested selection structure: when a selection structure’s true or false path contains another selection structure 2009 Pearson Education, Inc. All rights reserved.
Nested Selection Structures (cont'd. ) 2009 Pearson Education, Inc. All rights reserved.
The Voter Eligibility Application 2009 Pearson Education, Inc. All rights reserved.
The Voter Eligibility Application (cont’d. ) 2009 Pearson Education, Inc. All rights reserved.
Figure 5 -4: Flowchart showing the nested selection structure in the true path 2009 Pearson Education, Inc. All rights reserved.
Figure 5 -5: Flowchart showing the nested selection structure in the false path 2009 Pearson Education, Inc. All rights reserved.
The Voter Eligibility Application (cont’d. ) Figure 5 -6: Code for the flowcharts in Figures 5 -4 and 5 -5 2009 Pearson Education, Inc. All rights reserved.
Figure 5 -6: Code for the flowcharts in Figures 5 -4 and 5 -5 (cont’d. ) 2009 Pearson Education, Inc. All rights reserved.
Multiple-Alternative Selection Structures ■ Multiple-alternative selection structures: select structures that can choose from several alternatives 2009 Pearson Education, Inc. All rights reserved.
Multiple-Alternative Selection Structures (cont'd. ) 2009 Pearson Education, Inc. All rights reserved.
Multiple-Alternative Selection Structures (cont'd. ) Figure 5 -9: Pseudocode for the Display button’s Click event procedure 2009 Pearson Education, Inc. All rights reserved.
Figure 5 -10: Flowchart for the Display button’s Click event procedure 2009 Pearson Education, Inc. All rights reserved.
Two versions of the code corresponding to Figures 5 -9 and 5 -10 2009 Pearson Education, Inc. All rights reserved.
Two versions of the code corresponding to Figures 5 -9 and 5 -10 (cont’d. ) 2009 Pearson Education, Inc. All rights reserved.
If Then Else Statement ■ Look at an If. . . Then. . . Else multiple-selection statement that displays a text message based on a student’s grade: If grade = "A" Then display. Label. Text = "Excellent!" Else. If grade = "B" Then display. Label. Text = "Very good!" Else. If grade = "C" Then display. Label. Text = "Good. " Else. If grade = "D" Then display. Label. Text = "Poor. " Else. If grade = "F" Then display. Label. Text = "Failure. " Else display. Label. Text = "Invalid grade. " End If 2009 Pearson Education, Inc. All rights reserved.
The Select Case Statement ■ The following Select Case multiple-selection statement performs the same functionality as the previous If. . . Then. . . Else statement: Select Case grade Case "A" display. Label. Text Case "B" display. Label. Text Case "C" display. Label. Text Case "D" display. Label. Text Case "F" display. Label. Text Case Else display. Label. Text End Select = "Excellent!" = "Very good!" = "Good. " = "Poor. " = "Failure. " = "Invalid grade. " 2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
The Select Case Statement Used when there are many paths from which to choose Simpler and clearer than using several If…Then…Else statements Begins with Select Case, which specifies the value to be matched Ends with End Select Has one Case clause for each possible path Case Else is optional but must be the last clause in the statement 2009 Pearson Education, Inc. All rights reserved.
The Select Case Statement (cont'd. ) Case clause may have more than one value, separated by commas Only one value must be matched to process the code in this Case clause Processing of a Case clause code stops when the next Case clause is encountered If no values in Case clauses are matched, the Case Else clause is processed 2009 Pearson Education, Inc. All rights reserved.
Specifying a Range of Values in a Case Clause To and Is keywords: used to specify a range of values in a Case clause’s expression list To: When you know both the upper and lower bounds of the range Is: When you know only one end of the range Used with a comparison operator 2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
Using Radio Buttons in an Interface ■ Radio button control: allows the user to select only one of a group of two or more choices ■ Radio button choices are related but mutually exclusive; only one can be selected ■ Container control: ■ Isolates a group of radio buttons ■ Includes Group. Box, Panel, and Table. Layout controls 2009 Pearson Education, Inc. All rights reserved.
Using Radio Buttons in an Interface (cont'd. ) ■ Minimum number of radio buttons in a group is two ■ Must select a radio button to deselect another ■ Recommended maximum number in a group: seven ■ Windows standard is to set one as the default radio button ■ Shows as selected when the screen appears ■ Should be the most likely selection or the first radio button in the group ■ Set the Checked property to True to make it the default radio button 2009 Pearson Education, Inc. All rights reserved.
Using Radio Buttons in an Interface (cont'd. ) 2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
Using the Key. Press Event ■ Can prevent a text box from accepting an inappropriate character by coding the text box’s Key. Press event ■ Key. Press event: occurs each time the user presses a key while the control has the focus ■ Use the e parameter’s Key. Char property to determine the pressed key ■ Use the e parameter’s Handled property to cancel the key if it is inappropriate; set it to True to discard the character ■ Use Control. Chars. Back constant to represent the Backspace key on the keyboard 2009 Pearson Education, Inc. All rights reserved.
2009 Pearson Education, Inc. All rights reserved.
- Slides: 44