Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
1.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Programming with Microsoft Visual Basic th Edition
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Repeating Actions While and For Loops
Programming with Microsoft Visual Basic 2005, Third Edition
An Introduction to Programming with C++ Fifth Edition
Chapter 7: Sub and Function Procedures
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 5 new The Do…Loop Statement
CHAPTER SIX Loop Structures.
CHAPTER 6 Loop Structures.
CHAPTER SIX.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Chapter Four The Selection Structure
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 12: How Long Can This Go On?
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 4: The Selection Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
Chapter 6: The Repetition Structure
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Tutorial 6 The Repetition Structure
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 5: More on the Selection Structure
Computer Programming TCP1224 Chapter 8 More On Repetition Structure.
1.
Chapter 15 I’m on the Inside; You’re on the Outside (Nested Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Chapter 13 Do It, Then Ask Permission (Posttest Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Controlling Program Flow with Looping Structures
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 14 Do It, Then Ask Permission.
Unit 6 Repetition Processing Instructor: Brent Presley.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
Looping Structures. A B dialog box that pops up and prompts the user for input Text area that pops up and prompts the user for to wait while it gets.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 1 Unit 9 Do Until and For… Next Loops Chapter 5 Lists,
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Microsoft Visual Basic 2008: Reloaded Third Edition
Repeating Program Instructions
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
Presentation transcript:

Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes

Microsoft Visual Basic 2008: Reloaded, Third Edition2 Objectives After studying this chapter, you should be able to: Include the Do loop in both pseudocode and a flowchart Write a Do…Loop statement Initialize counters and accumulators Display a dialog box using the InputBox function Refresh the screen

Microsoft Visual Basic 2008: Reloaded, Third Edition3 Objectives (continued) Delay program execution Enable and disable a control

Microsoft Visual Basic 2008: Reloaded, Third Edition4 The Repetition Structure Repetition structure (or loop): a structure that repeatedly processes one or more program instructions until a condition is met Pretest loop –The condition is evaluated before the instructions within the loop are processed –The instructions may be processed zero or more times Posttest loop –The condition is evaluated after the instructions within the loop are processed –The instructions are always processed at least once

Microsoft Visual Basic 2008: Reloaded, Third Edition5 The Repetition Structure (continued) Repetition statements in Visual Basic –Do...Loop –For...Next –For Each...Next

Microsoft Visual Basic 2008: Reloaded, Third Edition6 The Do...Loop Statement Do...Loop statement: codes both a pretest or posttest loop Use While or Until to code the condition for the loop Repetition symbol in a flowchart is the diamond

Microsoft Visual Basic 2008: Reloaded, Third Edition7 The Do...Loop Statement (continued) Figure 6-1: How to use the Do…Loop statement

Microsoft Visual Basic 2008: Reloaded, Third Edition8 The Do...Loop Statement (continued) Figure 6-1: How to use the Do…Loop statement (continued)

The Do...Loop Statement (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition9 Figure 6-2: Processing steps for the pretest loop example

Microsoft Visual Basic 2008: Reloaded, Third Edition10 The Do...Loop Statement (continued) Figure 6-3: Processing steps for the posttest loop example

Microsoft Visual Basic 2008: Reloaded, Third Edition11 The Do...Loop Statement (continued) Figure 6-4: Pseudocode and flowchart for the pretest loop example

Microsoft Visual Basic 2008: Reloaded, Third Edition12 The Do...Loop Statement (continued) Figure 6-5: Pseudocode and flowchart for the posttest loop example

Microsoft Visual Basic 2008: Reloaded, Third Edition13 The Do...Loop Statement (continued) Figure 6-6: Examples showing that the pretest and posttest loops do not always produce the same results

Microsoft Visual Basic 2008: Reloaded, Third Edition14 Using Counters and Accumulators Counter: a numeric variable used for counting Accumulator: a numeric variable used for accumulating (adding together) Initializing: assigning a beginning value to a counter or accumulator variable Updating (or incrementing): adding a number to the value of a counter or accumulator variable Counters are always incremented by a constant value, usually 1

Microsoft Visual Basic 2008: Reloaded, Third Edition15 The InputBox Function Function: a predefined procedure that performs a specific task and returns a value InputBox function: displays a predefined dialog box that allows the user to enter data –Contains a text message, an OK button, a Cancel button, and an input area InputBox function returns: –The user’s entry if the user clicks the OK button –An empty string if the user clicks the Cancel button or the Close button on the title bar

Microsoft Visual Basic 2008: Reloaded, Third Edition16 The InputBox Function (continued) Figure 6-7: Example of a dialog box created by the InputBox function

Microsoft Visual Basic 2008: Reloaded, Third Edition17 The InputBox Function (continued) InputBox function parameters: –prompt: the message to display inside the dialog box –title: the text to display in the dialog box’s title bar –defaultResponse: a prefilled value for the user input

Microsoft Visual Basic 2008: Reloaded, Third Edition18 The InputBox Function (continued) Figure 6-8: How to use the InputBox function

Microsoft Visual Basic 2008: Reloaded, Third Edition19 The InputBox Function (continued) Figure 6-8: How to use the InputBox function (continued)

Microsoft Visual Basic 2008: Reloaded, Third Edition20 The Sales Express Application Requirements: display the average amount the company sold during the prior year Input: the amount of each salesperson’s sales Priming read: used to obtain the first input Must verify that a variable does not contain the value 0 before using it as a divisor

Microsoft Visual Basic 2008: Reloaded, Third Edition21 The Sales Express Application (continued) Figure 6-9: Pseudocode for the Sales Express application

Microsoft Visual Basic 2008: Reloaded, Third Edition22 The Sales Express Application (continued) Figure 6-10: Sales Entry dialog box Figure 6-11: Average sales amount displayed in the interface

Microsoft Visual Basic 2008: Reloaded, Third Edition23 Figure 6-2: Code for the calcButton in the Sales Express application

Microsoft Visual Basic 2008: Reloaded, Third Edition24 Using a List Box in an Interface List box: displays a list of choices from which the user can select zero or more choices SelectionMode property: controls the number of choices a user can select –None: user can scroll but not select anything –One: user can select one item –MultiSimple and MultiExtended: user can select multiple items

Microsoft Visual Basic 2008: Reloaded, Third Edition25 Adding Items to a List Box Items collection: a collection of the items in a list box Collection: a group of one or more individual objects treated as one unit Add method: adds an item to the list box’s Items collection –Items to be added must be converted to String Load event of a form: occurs when an application is started and the form is displayed for the first time

Microsoft Visual Basic 2008: Reloaded, Third Edition26 Adding Items to a List Box (continued) Figure 6-13: How to add items to a list box

Microsoft Visual Basic 2008: Reloaded, Third Edition27 Adding Items to a List Box (continued) Figure 6-14: Add methods entered in the form’s Load event procedure

Microsoft Visual Basic 2008: Reloaded, Third Edition28 Adding Items to a List Box (continued) Sorted property: –Determines if the list box items are sorted –Sort order is dictionary order Figure 6-15: Items added to the animalListBox and codeListBox

Accessing Items in a List Box Index: –A unique number that identifies an item in a collection –Is zero-relative: the first item has index of 0 Microsoft Visual Basic 2008: Reloaded, Third Edition29

Accessing Items in a List Box (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition30 Figure 6-16: How to access an item in a list box

Determining the Number of Items in a List Box Items.Count property: stores the number of items in a list box –Count value is always one higher than the highest index in the list box Microsoft Visual Basic 2008: Reloaded, Third Edition31

Microsoft Visual Basic 2008: Reloaded, Third Edition32 Figure 6-17: How to determine the number of items in a list box

Microsoft Visual Basic 2008: Reloaded, Third Edition33 The SelectedItem and SelectedIndex Properties SelectedItem property: –Contains the value of the selected item in the list –If nothing is selected, it contains the empty string SelectedIndex property: –Contains the index of the selected item in the list –If nothing is selected, it contains the value -1 Default list box item: the item that is selected by default when the interface first appears

Microsoft Visual Basic 2008: Reloaded, Third Edition34 The SelectedItem and SelectedIndex Properties (continued) Figure 6-18: Item selected in the animalListBox

Microsoft Visual Basic 2008: Reloaded, Third Edition35 Figure 6-19: How to use the SelectedItem and SelectedIndex properties

Microsoft Visual Basic 2008: Reloaded, Third Edition36 The SelectedItem and SelectedIndex Properties (continued) Figure 6-20: How to select the default list box item

Microsoft Visual Basic 2008: Reloaded, Third Edition37 The SelectedItem and SelectedIndex Properties (continued) Figure 6-21: Code to select the default item in each list box

Microsoft Visual Basic 2008: Reloaded, Third Edition38 The SelectedValueChanged and SelectedIndexChanged Events SelectedValueChanged and SelectedIndexChanged events: –Occur when a user selects an item in a list box –Occur when a code statement selects an item in a list box

Microsoft Visual Basic 2008: Reloaded, Third Edition39 The SelectedValueChanged and SelectedIndexChanged Events (continued) Figure 6-22: SelectedValueChanged and SelectedIndexChanged event procedures

The SelectedValueChanged and SelectedIndexChanged Events (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition40 Figure 6-23: Result of processing the SelectedValueChanged and SelectedIndexChanged event procedures

Microsoft Visual Basic 2008: Reloaded, Third Edition41 The Product Finder Application Allows the user to enter a product ID Searches for the ID in a list box If found, highlights the ID

Microsoft Visual Basic 2008: Reloaded, Third Edition42 The Product Finder Application (continued) Figure 6-24: Pseudocode for the Product Finder application

Microsoft Visual Basic 2008: Reloaded, Third Edition43 The Product Finder Application (continued) Figure 6-25: Sample run of the application when an ID is found Figure 6-26: Sample run of the application when an ID is not found

Microsoft Visual Basic 2008: Reloaded, Third Edition44 Figure 6-27: Code for the form’s Load event and findButton ’s Click event procedures

Microsoft Visual Basic 2008: Reloaded, Third Edition45 Programming Tutorial Figure 6-29: User interface Creating the Roll ‘Em Game Sleep method: delays execution of the program Refresh method: redraws the user interface

Microsoft Visual Basic 2008: Reloaded, Third Edition46 Programming Example Grade Calculator Figure 6-43: User interface

Microsoft Visual Basic 2008: Reloaded, Third Edition47 Summary The three programming structures are sequence, selection, and repetition Repetition structure (or loop): repeatedly processes a set of instructions Pretest loop tests the condition before the instructions are processed Posttest loop tests the condition after the instructions are processed

Microsoft Visual Basic 2008: Reloaded, Third Edition48 Summary (continued) Do...Loop statement: codes a pretest or posttest loop Use a While or Until condition in a Do...Loop Flowchart symbol for repetition is a diamond Counter and accumulators: variables that calculate subtotals, totals, and averages InputBox function: allows user input Verify that a variable does not contain a value of 0 before using it as a divisor

Microsoft Visual Basic 2008: Reloaded, Third Edition49 Summary (continued) List box: contains a minimum of three selections List box’s Items collection: contains the items in the list box Items.Add method: adds an item to the list Form’s Load event occurs before the form appears List box item’s index is used to access the item Items.Count property stores the number of items SelectedItem property of a list box: contains the value of the selected item in the list

Microsoft Visual Basic 2008: Reloaded, Third Edition50 Summary (continued) SelectedIndex property of a list box: contains the index position of the selected item in the list SelectedValueChanged and SelectedIndexChanged events occur when an item in a list box is selected Sleep method: delays program execution Me.Refresh : refreshes (redraws) the form Enabled property: used to enable or disable a control