Top Banner
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure
42
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 05

Microsoft Visual Basic 2010: Reloaded Fourth Edition

Chapter FiveMore on the Selection Structure

Page 2: Chapter 05

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

Page 3: Chapter 05

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

Page 4: Chapter 05

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

Page 5: Chapter 05

Nested Selection Structures (cont'd.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-1: Selection structures containing nested selection structures (cont’d.)

5

Page 6: Chapter 05

The Voter Eligibility Application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-2: Problem specification for the Voter Eligibility application

6

Page 7: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-3: Sample run of the Voter Eligibility application

The Voter Eligibility Application (cont’d.)

7

Page 8: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-4: Flowchart showing the nested selection structure in the true path

8

Page 9: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-5: Flowchart showing the nested selection structure in the false path

9

Page 10: Chapter 05

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

Page 11: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-6: Code for the flowcharts in Figures 5-4 and 5-5 (cont’d.)11

Page 12: Chapter 05

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

Page 13: Chapter 05

Multiple-Alternative Selection Structures (cont'd.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-8: Sample run of the Yardley Theater application

13

Page 14: Chapter 05

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

Page 15: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-9: Flowchart for the Display button’s Click event procedure

15

Page 16: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-10: Two versions of the code corresponding to Figures 5-9 and 5-10

16

Page 17: Chapter 05

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

Page 18: Chapter 05

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

Page 19: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-12: How to use the Select Case statement

19

Page 20: Chapter 05

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

Page 21: Chapter 05

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

Page 22: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-13: How to specify a range of values in a Case clause

22

Page 23: Chapter 05

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

Page 24: Chapter 05

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

Page 25: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-14: Gentry Supplies application’s interface

25

Using Radio Buttons in an Interface (cont'd.)

Page 26: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26

Figure 5-15: The Display button’s Click event procedure

Page 27: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-15: The Display button’s Click event procedure (cont'd.)

27

Page 28: Chapter 05

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

Page 29: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-16: How to use the MessageBox.Show method

29

Page 30: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-16: How to use the MessageBox.Show method (cont’d.)

30

Page 31: Chapter 05

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

Page 32: Chapter 05

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

Page 33: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33

Figure 5-19: How to use the MessageBox.Show method’s return value

Page 34: Chapter 05

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

Page 35: Chapter 05

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

Page 36: Chapter 05

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-20: How to use the KeyPress event to cancel invalid characters

36

Page 37: Chapter 05

Programming Tutorial 1

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-23: MainForm for the Rock, Paper, Scissors Game application

37

Page 38: Chapter 05

Programming Tutorial 2

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-34: MainForm for the Charleston Cable Company application

38

Page 39: Chapter 05

Programming Example

Microsoft Visual Basic 2010: Reloaded, Fourth Edition

Figure 5-40: MainForm in the CD Emporium application

39

Page 40: Chapter 05

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

Page 41: Chapter 05

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

Page 42: Chapter 05

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


Related Documents