Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.

Similar presentations


Presentation on theme: "Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure."— Presentation transcript:

1 Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure

2 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 Edition2

3 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 Edition3

4 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

5 Nested Selection Structures (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition5 Figure 5-1: Sample run of the Voter Eligibility application

6 Microsoft Visual Basic 2008: Reloaded, Third Edition6 Figure 5-2: Pseudocode and code showing the nested selection structure in the true path

7 Microsoft Visual Basic 2008: Reloaded, Third Edition7 Figure 5-3: Flowchart showing the nested selection structure in the true path

8 Microsoft Visual Basic 2008: Reloaded, Third Edition8 Figure 5-4: Pseudocode and code showing the nested selection structure in the false path

9 Microsoft Visual Basic 2008: Reloaded, Third Edition9 Figure 5-5: Flowchart showing the nested selection structure in the false path

10 The If/ElseIf/Else Selection Structure Example: a procedure to display a message based on a letter grade Microsoft Visual Basic 2008: Reloaded, Third Edition10

11 The If/ElseIf/Else Selection Structure (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition11 Figure 5-6: Sample run of the Grade Message application

12 The If/ElseIf/Else Selection Structure (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition12 Figure 5-7: Two versions of the messageButton’s Click event procedure

13 Microsoft Visual Basic 2008: Reloaded, Third Edition13 Figure 5-7: Two versions of the messageButton’s Click event procedure (continued)

14 Microsoft Visual Basic 2008: Reloaded, Third Edition14 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

15 Microsoft Visual Basic 2008: Reloaded, Third Edition15 The Case Selection Structure (continued) Figure 5-8: Pseudocode showing the Case selection structure

16 Microsoft Visual Basic 2008: Reloaded, Third Edition16 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

17 Microsoft Visual Basic 2008: Reloaded, Third Edition17 The Case Selection Structure (continued) Figure 5-9: Flowchart showing the Case selection structure

18 Microsoft Visual Basic 2008: Reloaded, Third Edition18 Figure 5-10: How to use the Select Case statement

19 Microsoft Visual Basic 2008: Reloaded, Third Edition19 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

20 Microsoft Visual Basic 2008: Reloaded, Third Edition20 Figure 5-11: Example of using the To and Is keywords in a Select Case statement

21 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 Edition21

22 Using Radio Buttons (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition22 Figure 5-12: Sample run of the Gentry Supplies application

23 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 Edition23

24 Microsoft Visual Basic 2005: Reloaded, Second Edition24 Figure 5-13: The displayButton’s Click event procedure

25 Using Radio Buttons (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition25 Figure 5-13: The displayButton’s Click event procedure (continued)

26 Microsoft Visual Basic 2008: Reloaded, Third Edition26 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

27 Microsoft Visual Basic 2008: Reloaded, Third Edition27 Figure 5-14: How to use the MessageBox.Show method

28 The MessageBox.Show Method (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition28 Figure 5-15: Message box displayed by Example 1 in Figure 5-14

29 The MessageBox.Show Method (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition29 Figure 5-16: Message box displayed by Example 2 in Figure 5-14

30 Microsoft Visual Basic 2008: Reloaded, Third Edition30 Figure 5-17: How to use the value returned by the MessageBox.Show method

31 Coding the KeyPress Event Microsoft Visual Basic 2008: Reloaded, Third Edition31 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

32 Coding the KeyPress Event (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition32 Figure 5-18: How to use the KeyPress event

33 Programming Tutorial Microsoft Visual Basic 2008: Reloaded, Third Edition33 Figure 5-20: User interface

34 Programming Example Microsoft Visual Basic 2008: Reloaded, Third Edition34 Figure 5-31: User interface

35 Microsoft Visual Basic 2008: Reloaded, Third Edition35 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

36 Microsoft Visual Basic 2008: Reloaded, Third Edition36 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

37 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 Edition37


Download ppt "Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure."

Similar presentations


Ads by Google