Académique Documents
Professionnel Documents
Culture Documents
Fourth Edition
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
Objectives (cont'd.)
Display a message in a message box Prevent the entry of invalid characters in a text box
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
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 (contd.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11
13
Figure 5-9: Pseudocode for the Display buttons Click event procedure
14
Figure 5-9: Flowchart for the Display buttons 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 (contd.)
17
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
Is:
When you know only one end of the range Used with a comparison operator
21
23
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
25
26
27
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
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28
29
Figure 5-17: Message box displayed by the code in Example 1 in Figure 5-16
31
Figure 5-18: Message box displayed by the code in Example 2 in Figure 5-14
32
33
35
Figure 5-20: How to use the KeyPress event to cancel invalid characters
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36
Programming Tutorial 1
Figure 5-23: MainForm for the Rock, Paper, Scissors Game application
37
Programming Tutorial 2
38
Programming Example
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 IfThenElse 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.)
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 parameters Handled property to True constant to discard the character
42