Microsoft Visual Basic 2010 Reloaded Fourth Edition Chapter
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure
Objectives After studying this chapter, you should be able to: • Include a nested selection structure in pseudocode and in a flowchart • Code a nested selection structure • Include a multiple-alternative selection structure in pseudocode and in a flowchart • Code a multiple-alternative selection structure • Include radio buttons in an interface Microsoft Visual Basic 2010: Reloaded, Fourth Edition 2
Objectives (cont'd. ) • Display a message in a message box • Prevent the entry of invalid characters in a text box Microsoft Visual Basic 2010: Reloaded, Fourth Edition 3
Making More Than One Decision • Nested selection structure: when a selection structure’s true or false path contains another selection structure Figure 5 -1: Selection structures containing nested selection structures Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4
Nested Selection Structures (cont'd. ) Figure 5 -1: Selection structures containing nested selection structures (cont’d. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5
The Voter Eligibility Application Figure 5 -2: Problem specification for the Voter Eligibility application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6
The Voter Eligibility Application (cont’d. ) Figure 5 -3: Sample run of the Voter Eligibility application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7
Figure 5 -4: Flowchart showing the nested selection structure in the true path Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8
Figure 5 -5: Flowchart showing the nested selection structure in the false path Microsoft Visual Basic 2010: Reloaded, Fourth Edition 9
The Voter Eligibility Application (cont’d. ) Figure 5 -6: Code for the flowcharts in Figures 5 -4 and 5 -5 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10
Figure 5 -6: Code for the flowcharts in Figures 5 -4 and 5 -5 (cont’d. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11
Multiple-Alternative Selection Structures • Multiple-alternative selection structures: select structures that can choose from several alternatives Figure 5 -7: Problem specification for the Yardley Theater application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12
Multiple-Alternative Selection Structures (cont'd. ) Figure 5 -8: Sample run of the Yardley Theater application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13
Multiple-Alternative Selection Structures (cont'd. ) Figure 5 -9: Pseudocode for the Display button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 14
Figure 5 -9: Flowchart for the Display button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15
Figure 5 -10: Two versions of the code corresponding to Figures 5 -9 and 5 -10 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 16
Figure 5 -10: Two versions of the code corresponding to Figures 5 -9 and 5 -10 (cont’d. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 17
The Select Case Statement • 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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 18
Figure 5 -12: How to use the Select Case statement Microsoft Visual Basic 2010: Reloaded, Fourth Edition 19
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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 20
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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 21
Figure 5 -13: How to specify a range of values in a Case clause Microsoft Visual Basic 2010: Reloaded, Fourth Edition 22
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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23
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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24
Using Radio Buttons in an Interface (cont'd. ) Figure 5 -14: Gentry Supplies application’s interface Microsoft Visual Basic 2010: Reloaded, Fourth Edition 25
Figure 5 -15: The Display button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26
Figure 5 -15: The Display button’s Click event procedure (cont'd. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27
The Message. Box. Show Method • Message. Box. Show method: – Displays a message box with text, one or more buttons, and an icon • When a message box is displayed, the program waits until the user selects a button • Message. Box. Show returns an integer value indicating which button the user selected • Dialog. Result values include: – Windows. Forms. Dialog. Result. Yes – Windows. Forms. Dialog. Result. No Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28
Figure 5 -16: How to use the Message. Box. Show method Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29
Figure 5 -16: How to use the Message. Box. Show method (cont’d. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30
The Message. Box. Show Method (cont'd. ) Figure 5 -17: Message box displayed by the code in Example 1 in Figure 5 -16 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31
The Message. Box. Show Method (cont'd. ) Figure 5 -18: Message box displayed by the code in Example 2 in Figure 5 -14 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32
Figure 5 -19: How to use the Message. Box. Show method’s return value Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33
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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34
Using the Key. Press Event (cont’d. ) • Use Control. Chars. Back constant to represent the Backspace key on the keyboard • Line continuation character: the underscore • Allows you to split a line of code into two lines in the code editor Microsoft Visual Basic 2010: Reloaded, Fourth Edition 35
Figure 5 -20: How to use the Key. Press event to cancel invalid characters Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36
Programming Tutorial 1 Figure 5 -23: Main. Form for the Rock, Paper, Scissors Game application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 37
Programming Tutorial 2 Figure 5 -34: Main. Form for the Charleston Cable Company application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 38
Programming Example Figure 5 -40: Main. Form in the CD Emporium application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 39
Summary • Selection structures can be nested in either the true or false path of another selection structure • Primary decision is made by the outer selection structure, while the secondary decision is made by the inner (nested) selection structure • Use If…Then…Else or Select Case statements for multiple-alternative selection structures • Diamond symbol represents the condition in a multiple-alternative selection structures – Each flowline represents a possible path Microsoft Visual Basic 2010: Reloaded, Fourth Edition 40
Summary (cont'd. ) • Each Case clause in Select Case statement represents a possible path • Use To keyword to specify a range of valid values when both the lower and upper bounds are known • Use Is keyword with a comparison operator to specify a lower or upper bound but not both • Use radio buttons to limit the user to one choice from a group of two or more related but mutually exclusive choices • Use a container to isolate groups of radio buttons Microsoft Visual Basic 2010: Reloaded, Fourth Edition 41
Summary (cont'd. ) • Message. Box. Show method allows an application to communicate with the user • Message. Box. Show method returns an integer indicating which button was chosen by the user • Use the Key. Press event of a text box to prevent it from accepting an inappropriate character – Set the e parameter’s Handled property to True constant to discard the character Microsoft Visual Basic 2010: Reloaded, Fourth Edition 42
- Slides: 42