Upload
mark-gibson
View
217
Download
0
Embed Size (px)
Citation preview
Objectives
After studying this chapter, you should be able to:
• Include a nested selection structure in pseudocode and in a flowchart
• Code an If/ElseIf/Else selection structure
• Include a Case selection structure in pseudocode and in a flowchart
• Code a Case selection structure
• Include radio buttons in an interface
Microsoft Visual Basic 2008: Reloaded, Third Edition 2
Objectives (continued)
• Display a message in a message box
• Prevent the entry of unwanted characters in a text box
Microsoft Visual Basic 2008: Reloaded, Third Edition 3
Microsoft Visual Basic 2008: Reloaded, Third Edition 4
Nested Selection Structures
• Nested selection structure: a selection structure that is completely contained within another selection structure
• Primary decision: decision made by the outer selection structure
• Secondary decision: decision made by the inner selection structure
Nested Selection Structures (continued)
Microsoft Visual Basic 2008: Reloaded, Third Edition 5
Figure 5-1: Sample run of the Voter Eligibility application
Microsoft Visual Basic 2008: Reloaded, Third Edition 6
Figure 5-2: Pseudocode and code showing the nested selection structure in the true path
Microsoft Visual Basic 2008: Reloaded, Third Edition 7
Figure 5-3: Flowchart showing the nested selection structure in the true path
Microsoft Visual Basic 2008: Reloaded, Third Edition 8
Figure 5-4: Pseudocode and code showing the nested selection structure in the false path
Microsoft Visual Basic 2008: Reloaded, Third Edition 9
Figure 5-5: Flowchart showing the nested selection structure in the false path
The If/ElseIf/Else Selection Structure
• Example: a procedure to display a message based on a letter grade
Microsoft Visual Basic 2008: Reloaded, Third Edition 10
The If/ElseIf/Else Selection Structure (continued)
Microsoft Visual Basic 2008: Reloaded, Third Edition 11
Figure 5-6: Sample run of the Grade Message application
The If/ElseIf/Else Selection Structure (continued)
Microsoft Visual Basic 2008: Reloaded, Third Edition 12
Figure 5-7: Two versions of the messageButton’s Click event procedure
Microsoft Visual Basic 2008: Reloaded, Third Edition 13
Figure 5-7: Two versions of the messageButton’s Click event procedure (continued)
Microsoft Visual Basic 2008: Reloaded, Third Edition 14
The Case Selection Structure
• Case selection structure: – Used when there are many paths from which to
choose– Simpler and clearer than using If/ElseIf/Else– Flowchart symbol is the same as the symbol for If,
If/Else, and If/ElseIf/Else
Microsoft Visual Basic 2008: Reloaded, Third Edition 15
The Case Selection Structure (continued)
Figure 5-8: Pseudocode showing the Case selection structure
Microsoft Visual Basic 2008: Reloaded, Third Edition 16
The Case Selection Structure (continued)
• Case selection structure in a flowchart:– Uses the diamond symbol– One flowline into the diamond, but many flowlines
out of the diamond– Each flowline represents a possible path
• Case selection structure evaluates an expression to determine which path to take
• Uses the Select Case statement:– Begins with Select Case– Ends with End Select– Has one Case clause for each possible value
Microsoft Visual Basic 2008: Reloaded, Third Edition 17
The Case Selection Structure (continued)
Figure 5-9: Flowchart showing the Case selection structure
Microsoft Visual Basic 2008: Reloaded, Third Edition 18
Figure 5-10: How to use the Select Case statement
Microsoft Visual Basic 2008: Reloaded, Third Edition 19
Specifying a Range of Values in an ExpressionList
• To and Is keywords: 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 2008: Reloaded, Third Edition 20
Figure 5-11: Example of using the To and Is keywords in a Select Case statement
Using Radio Buttons
• RadioButton control: allows the user to select only one of a group of two or more choices
• RadioButton 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 2005: Reloaded, Second Edition 21
Using Radio Buttons (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition 22
Figure 5-12: Sample run of the Gentry Supplies application
Using Radio Buttons (continued)
• 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 2005: Reloaded, Second Edition 23
Microsoft Visual Basic 2005: Reloaded, Second Edition 24
Figure 5-13: The displayButton’s Click event procedure
Using Radio Buttons (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition 25
Figure 5-13: The displayButton’s Click event procedure (continued)
Microsoft Visual Basic 2008: Reloaded, Third Edition 26
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 2008: Reloaded, Third Edition 27
Figure 5-14: How to use the MessageBox.Show method
The MessageBox.Show Method (continued)
Microsoft Visual Basic 2008: Reloaded, Third Edition 28
Figure 5-15: Message box displayed by Example 1 in Figure 5-14
The MessageBox.Show Method (continued)
Microsoft Visual Basic 2008: Reloaded, Third Edition 29
Figure 5-16: Message box displayed by Example 2 in Figure 5-14
Microsoft Visual Basic 2008: Reloaded, Third Edition 30
Figure 5-17: How to use the value returned by the MessageBox.Show method
Coding the KeyPress Event
Microsoft Visual Basic 2008: Reloaded, Third Edition 31
• 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• Use ControlChars.Back constant to represent
the Backspace key on the keyboard
Coding the KeyPress Event (continued)
Microsoft Visual Basic 2008: Reloaded, Third Edition 32
Figure 5-18: How to use the KeyPress event
Programming Tutorial
Microsoft Visual Basic 2008: Reloaded, Third Edition 33
Figure 5-20: User interface
Programming Example
Microsoft Visual Basic 2008: Reloaded, Third Edition 34
Figure 5-31: User interface
Microsoft Visual Basic 2008: Reloaded, Third Edition 35
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/ElseIf/Else or Case structures when there are several possible alternative outcomes
• Select Case statement is used to code the Case selection structure
Microsoft Visual Basic 2008: Reloaded, Third Edition 36
Summary (continued)
• 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
• MessageBox.Show method allows an application to communicate with the user
Summary (continued)
• MessageBox.Show method returns an integer indicating which button was chosen by the user
• Use sentence capitalization for the text message in the MessageBox.Show method, but book title capitalization for the caption
• Use the KeyPress event of a text box to prevent it from accepting an inappropriate character
• Use the ControlChars.Back constant to represent the Backspace key on the keyboard
Microsoft Visual Basic 2008: Reloaded, Third Edition 37