420 likes | 432 Views
Explore nested and multiple-alternative selection structures, along with using radio buttons and preventing invalid entries in text boxes in Visual Basic 2010. Learn to code and create flowcharts effectively.
E N D
Microsoft Visual Basic 2010: ReloadedFourth 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
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
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
Nested Selection Structures (cont'd.) Figure 5-1: Selection structures containing nested selection structures (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The Voter Eligibility Application Figure 5-2: Problem specification for the Voter Eligibility application Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The Voter Eligibility Application (cont’d.) Figure 5-3: Sample run of the Voter Eligibility application Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-4: Flowchart showing the nested selection structure in the true path Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-5: Flowchart showing the nested selection structure in the false path Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Figure 5-6: Code for the flowcharts in Figures 5-4 and 5-5 (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Multiple-Alternative Selection Structures (cont'd.) Figure 5-8: Sample run of the Yardley Theater application Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
Figure 5-9: Flowchart for the Display button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-10: Two versions of the code corresponding to Figures 5-9 and 5-10 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.) 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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-12: How to use the Select Case statement 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 CaseElse clause is processed 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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-13: How to specify a range of values in a Case clause Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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
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
Using Radio Buttons in an Interface (cont'd.) Figure 5-14: Gentry Supplies application’s interface Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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.) 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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-16: How to use the MessageBox.Show method Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-16: How to use the MessageBox.Show method (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition
The MessageBox.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
The MessageBox.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
Figure 5-19: How to use the MessageBox.Show method’s return value Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Using the KeyPress Event • 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’sKeyChar 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
Using the KeyPress Event (cont’d.) • 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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 5-20: How to use the KeyPress event to cancel invalid characters Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Programming Tutorial 1 Figure 5-23: MainForm for the Rock, Paper, Scissors Game application Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Programming Tutorial 2 Figure 5-34: MainForm for the Charleston Cable Company application Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Programming Example Figure 5-40: MainForm in the CD Emporium application 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 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 Microsoft Visual Basic 2010: Reloaded, Fourth Edition
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