1 / 44

Microsoft Visual Basic: Reloaded

Microsoft Visual Basic: Reloaded. Chapter Five More on the Selection Structure. 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

millerlinda
Download Presentation

Microsoft Visual Basic: Reloaded

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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. Dental Payment Application • A CheckBox is a small square that either is blank or contains a check mark ( ). CheckBox controls (unchecked)

  4. Designing the DentalPayment 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. Using CheckBoxes • A CheckBox is known as a statebutton because it can be in the on/off [true/false] state. • The text that appears alongside a CheckBox is called the CheckBoxlabel. • If the CheckBox is checked, the Checkedproperty contains the Boolean value True; otherwise, it contains False.

  6. Using a Dialog to Display a Message • This dialog contains: • a title bar • a close box • a message • an OK button • an icon indicating the tone of the message Close box Title bar Icon indicates the tone of the message Dialog sized to accommodate contents OKButton allows theuser to close the dialog

  7. Using a Dialog to Display a Message Figure 8.11| Message dialog code that displays a message to users.

  8. Applying these concepts • Revise the code so that users must enter a name and select at least one CheckBox before they click the CalculateButton. 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

  9. Making More Than One Decision • Nested selection structure: when a selection structure’s true or false path contains another selection structure

  10. Nested Selection Structures (cont'd.)

  11. The Voter Eligibility Application

  12. The Voter Eligibility Application (cont’d.)

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

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

  15. The Voter Eligibility Application (cont’d.) Figure 5-6: Code for the flowcharts in Figures 5-4 and 5-5

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

  17. Multiple-Alternative Selection Structures • Multiple-alternative selection structures: select structures that can choose from several alternatives

  18. Multiple-Alternative Selection Structures (cont'd.)

  19. Multiple-Alternative Selection Structures (cont'd.) Figure 5-9: Pseudocode for the Display button’s Click event procedure

  20. Figure 5-10: Flowchart for the Display button’s Click event procedure

  21. Two versions of the code corresponding to Figures 5-9 and 5-10

  22. Two versions of the code corresponding to Figures 5-9 and 5-10 (cont’d.)

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

  24. The Select Case Statement • The following SelectCase multiple-selection statement performs the same functionality as the previous If...Then...Else statement: SelectCase gradeCase"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."CaseElse displayLabel.Text = "Invalid grade."EndSelect

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

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

  31. 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

More Related