Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2  2009 Pearson Education, Inc. All rights reserved. ■The Dental Payment Application ■Using CheckBoxes ■Message Dialog Windows ■Nested selection structure ■Multiple-alternative selection structure ■Select Case statement ■Include radio buttons in an interface ■Prevent the entry of invalid characters in a text box Overview

3  2009 Pearson Education, Inc. All rights reserved. ■A CheckBox is a small square that either is blank or contains a check mark ( ). CheckBox controls (unchecked) Dental Payment Application

4  2009 Pearson Education, Inc. All rights reserved. Designing the Dental Payment Application When the user clicks the Calculate Button Clear previous output If user has not entered a patient name or has not selected any CheckBoxes Display message in dialog Else Initialize the total to zero If “Cleaning” CheckBox is selected Add cost of a cleaning to the total If “Cavity Filling” CheckBox is selected Add cost of receiving a cavity filling to the total If “X-Ray” CheckBox is selected Add cost of receiving an x-ray to the total Format total to be displayed as currency Display total

5  2009 Pearson Education, Inc. All rights reserved. ■A CheckBox is known as a state button because it can be in the on/off [true/false] state. ■The text that appears alongside a CheckBox is called the CheckBox label. ■If the CheckBox is checked, the Checked property contains the Boolean value True ; otherwise, it contains False. Using CheckBoxes

6  2009 Pearson Education, Inc. All rights reserved.

7 ■This dialog contains: a title bar a close box a message an OK button an icon indicating the tone of the message Using a Dialog to Display a Message OK Button allows the user to close the dialog Dialog sized to accommodate contents Close box Title bar Icon indicates the tone of the message

8  2009 Pearson Education, Inc. All rights reserved. Using a Dialog to Display a Message Figure 8.11 | Message dialog code that displays a message to users.

9  2009 Pearson Education, Inc. All rights reserved.

10

11

12

13 ■Revise the code so that users must enter a name and select at least one CheckBox before they click the Calculate Button. If they don’t an error dialog will be displayed. ■Use If Then Else, OrElse and AndAlso to determine if name is entered and at least one CheckBox is selected. ■cleanCheckBox ■xrayCheckBox ■cavityCheckBox ■nameTextBox Applying these concepts

14  2009 Pearson Education, Inc. All rights reserved.

15

16

17 Making More Than One Decision ■Nested selection structure: when a selection structure’s true or false path contains another selection structure

18  2009 Pearson Education, Inc. All rights reserved. Nested Selection Structures (cont'd.)

19  2009 Pearson Education, Inc. All rights reserved. The Voter Eligibility Application

20  2009 Pearson Education, Inc. All rights reserved. The Voter Eligibility Application (cont’d.)

21  2009 Pearson Education, Inc. All rights reserved. Figure 5-4: Flowchart showing the nested selection structure in the true path

22  2009 Pearson Education, Inc. All rights reserved. Figure 5-5: Flowchart showing the nested selection structure in the false path

23  2009 Pearson Education, Inc. All rights reserved. Figure 5-6: Code for the flowcharts in Figures 5-4 and 5-5 The Voter Eligibility Application (cont’d.)

24  2009 Pearson Education, Inc. All rights reserved. Figure 5-6: Code for the flowcharts in Figures 5-4 and 5-5 (cont’d.)

25  2009 Pearson Education, Inc. All rights reserved. Multiple-Alternative Selection Structures ■Multiple-alternative selection structures: select structures that can choose from several alternatives

26  2009 Pearson Education, Inc. All rights reserved. Multiple-Alternative Selection Structures (cont'd.)

27  2009 Pearson Education, Inc. All rights reserved. Multiple-Alternative Selection Structures (cont'd.) Figure 5-9: Pseudocode for the Display button’s Click event procedure

28  2009 Pearson Education, Inc. All rights reserved. Figure 5-10: Flowchart for the Display button’s Click event procedure

29  2009 Pearson Education, Inc. All rights reserved. Two versions of the code corresponding to Figures 5-9 and 5-10

30  2009 Pearson Education, Inc. All rights reserved. Two versions of the code corresponding to Figures 5-9 and 5-10 (cont’d.)

31  2009 Pearson Education, Inc. All rights reserved. If Then Else Statement ■Look at an If...Then...Else multiple-selection statement that displays a text message based on a student’s grade: If grade = "A" Then displayLabel.Text = "Excellent!" ElseIf grade = "B" Then displayLabel.Text = "Very good!" ElseIf grade = "C" Then displayLabel.Text = "Good." ElseIf grade = "D" Then displayLabel.Text = "Poor." ElseIf grade = "F" Then displayLabel.Text = "Failure." Else displayLabel.Text = "Invalid grade." End If

32  2009 Pearson Education, Inc. All rights reserved. The Select Case Statement ■The following Select Case multiple-selection statement performs the same functionality as the previous If...Then...Else statement: Select Case grade Case "A" displayLabel.Text = "Excellent!" Case "B" displayLabel.Text = "Very good!" Case "C" displayLabel.Text = "Good." Case "D" displayLabel.Text = "Poor." Case "F" displayLabel.Text = "Failure." Case Else displayLabel.Text = "Invalid grade." End Select

33  2009 Pearson Education, Inc. All rights reserved.

34 The 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

35  2009 Pearson Education, Inc. All rights reserved. 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

36  2009 Pearson Education, Inc. All rights reserved. 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

37  2009 Pearson Education, Inc. All rights reserved.

38 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

39  2009 Pearson Education, Inc. All rights reserved. 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

40  2009 Pearson Education, Inc. All rights reserved. Using Radio Buttons in an Interface (cont'd.)

41  2009 Pearson Education, Inc. All rights reserved.

42

43 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’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 ■Use ControlChars.Back constant to represent the Backspace key on the keyboard

44  2009 Pearson Education, Inc. All rights reserved.


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

Similar presentations


Ads by Google