Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Chapter FiveMore 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
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
4
Nested Selection Structures (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-1: Selection structures containing nested selection structures (cont’d.)
5
The Voter Eligibility Application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-2: Problem specification for the Voter Eligibility application
6
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-3: Sample run of the Voter Eligibility application
The Voter Eligibility Application (cont’d.)
7
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-4: Flowchart showing the nested selection structure in the true path
8
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-5: Flowchart showing the nested selection structure in the false path
9
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-6: Code for the flowcharts in Figures 5-4 and 5-5
The Voter Eligibility Application (cont’d.)
10
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-6: Code for the flowcharts in Figures 5-4 and 5-5 (cont’d.)11
Multiple-Alternative Selection Structures
• Multiple-alternative selection structures: select structures that can choose from several alternatives
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12
Figure 5-7: Problem specification for the Yardley Theater application
Multiple-Alternative Selection Structures (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-8: Sample run of the Yardley Theater application
13
Multiple-Alternative Selection Structures (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-9: Pseudocode for the Display button’s Click event procedure
14
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-9: Flowchart for the Display button’s Click event procedure
15
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-10: Two versions of the code corresponding to Figures 5-9 and 5-10
16
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-10: Two versions of the code corresponding to Figures 5-9 and 5-10 (cont’d.)
17
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
18
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-12: How to use the Select Case statement
19
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
20
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
21
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-13: How to specify a range of values in a Case clause
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 GroupBox, Panel, and TableLayout 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 buttonMicrosoft Visual Basic 2010: Reloaded, Fourth Edition 24
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-14: Gentry Supplies application’s interface
25
Using Radio Buttons in an Interface (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26
Figure 5-15: The Display button’s Click event procedure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-15: The Display button’s Click event procedure (cont'd.)
27
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The MessageBox.Show Method
• MessageBox.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• MessageBox.Show returns an integer value
indicating which button the user selected• DialogResult values include:
– Windows.Forms.DialogResult.Yes– Windows.Forms.DialogResult.No
28
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-16: How to use the MessageBox.Show method
29
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-16: How to use the MessageBox.Show method (cont’d.)
30
The MessageBox.Show Method (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-17: Message box displayed by the code in Example 1 in Figure 5-16
31
The MessageBox.Show Method (cont'd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-18: Message box displayed by the code in Example 2 in Figure 5-14
32
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33
Figure 5-19: How to use the MessageBox.Show method’s return value
Using the KeyPress Event
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
• Can prevent a text box from accepting an inappropriate character by coding the text box’s KeyPress event
• KeyPress event: occurs each time the user presses a key while the control has the focus• Use the e parameter’s KeyChar 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
34
Using the KeyPress Event (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
• Use ControlChars.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
35
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-20: How to use the KeyPress event to cancel invalid characters
36
Programming Tutorial 1
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-23: MainForm for the Rock, Paper, Scissors Game application
37
Programming Tutorial 2
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-34: MainForm for the Charleston Cable Company application
38
Programming Example
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-40: MainForm in the CD Emporium application
39
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
40
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
41
Summary (cont'd.)
• MessageBox.Show method allows an application to communicate with the user
• MessageBox.Show method returns an integer indicating which button was chosen by the user
• Use the KeyPress 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