Title: Microsoft Visual Basic 2005: Reloaded Second Edition
1Microsoft Visual Basic 2005 Reloaded Second
Edition
- Chapter 5
- Repeating Program Instructions
2Objectives
- Write loops using For...Next and Do...Loop
- Display a dialog box using the InputBox function
Include a list box and a combo box in an
interface - Initialize and update counters and accumulators
- Create a multiline text box that cannot be edited
3We use loops because computers are really good at
doing the same thing over over over again...
4The Repetition Structure (loop)
- This is a structure that repeatedly/iteratively
processes one or more program instructions
usually until some condition is met - Pretest loop
- The condition is evaluated before the
instructions within the loop are processed - The instructions may never be processed
- Posttest loop
- The condition is evaluated after the instructions
within the loop are processed - The instructions are always processed at least
once
5Types of loops in VB
- For...Next
- Used when we know how many times the loop needs
to iterate the stopping condition is the number
of times the loop needs to run - Pretest
- Do...Loop
- Used when we dont know how many iterations are
needed the stopping condition is not a number - Pretest (while) or posttest (until)
- More general than the ForNext
6ForNext example
- Dim sum As Integer 0
- Dim num As Integer
- For num 1 To 5
- sum num sum
- Next num
- sumLabel.Text Convert.ToString(sum)
- What value is displayed in the label?
7More ForNext examples
- Compute n!
- Ask a user to enter 10 integers, one at a time,
and calculate their sum their product. Or,
find the largest in the list the smallest in the
list. - Can repeatedly reuse a text box for this input,
or better to use an input box - Ask a user to enter two integers and calculate
the sum (or product) of all the integers between
the two integers.
8The For...Next Statement
A nice way to display results from each iteration
9The For...Next Statement syntax
- For num startvalue To endvalue Step stepvalue
- sum num sum
- Next num
- Startvalue, endvalue, and stepvalue items
- Control the number of times the loop is processed
- Must evaluate to numeric values
- Can be positive or negative
- If positive, its a countup
- If negative, its a countdown
10What if we want to display something for each
iteration of the loop?
- We need a way of filling up a text box
incrementally. - Can do it horizontally 1 2 3 4 5
- Or vertically 1
- 2
- 3
- 4
- 5
11Horizontally
- We must build the output string as we go
- For num 1 To 5
- evenNumsLabel.Text
- evenNumsLabel.Text num.ToString
- Next num
12Vertically
- We must build the output string as we go
- For num 1 To 5
- evenNumsLabel.Text
- evenNumsLabel.Text num.ToString
ControlChars.NewLine - Next num
Whats this?
Whats this?
13All three loops in VB
- While
- entrance controlled - pretest
- Until
- exit controlled - posttest
- For Next
- Counting - pretest
14How they are alike
- They all have the format
- loop control
- loop body
- loop control
- They all cause the statements in the loop body to
be executed repeatedly - loop control contains the condition for loop
termination
15But each type of loop has its own special
features
- Sometimes its appropriate - even correct - to
use one type over the others - Often, the choice is yours
- Lets see how they compare with each other...
16While loops - pretest
- Do While condition
- loop body
- Loop
- The condition is checked before the loop body is
executed. - It must be initialized outside the loop.
- The loop body must contain an instruction that
affects the condition. - Otherwise, the loop will be infinite!
17Pretest
- Dim Number As Integer
- Number 1
- Do While number lt 3
- messageBox.Show (number.ToString)
- number number 1
- Loop
18The pretest Do...Loop
19Until loops - posttest
- Do
- loop body
- Loop Until condition
- The condition is checked after the loop body is
executed. - It does not have to initialized outside the loop
- The loop body must contain an instruction that
affects the condition. - Otherwise, the loop will be infinite!
20Posttest
- Dim Number As Integer
- number 1
- Do
- messageBox.Show (number.ToString)
- number number 1
- Loop Until number gt 3
21The posttest Do...Loop
22When to use one or the other?
- If you dont want the loop body to be executed
even once before the condition is checked - If you want the loop body to be executed at least
once before the condition is ever checked - If you know how many times the loop body should
be executed...
23Examples
Problem from last time
- Display the evens between 2 and 8 using
- ForNext
- Do While
- Do Until
- Which do you like the most for this application?
Why?
24How to check for correct password?
- Which loop to use?
- How many tries will we give the user?
- First, lets do unlimited
- Then three tries
25Read in data from user inside the loop
- Good idea to use input box.
- Processing average of input values involves
- A counter
- An accumulator
- Both must be initialized
- Both must be updated
26The Sales Express Application
Use input box
Use input box
27The InputBox Function
- This function displays a predefined dialog box
that contains - a text message
- an OK button
- a Cancel button
- an input area
- The InputBox function returns
- The users entry if the user clicks OK
- An empty string if the user clicks Cancel or Close
28The InputBox Function
29Loops and List Boxes
A match made in heaven!
A match made in heaven!
30Using a List Box in an Interface
- For now, well learn how to create and fill them
- Next time, well use loops to process through
them - The ListBox tool creates a ListBox control that
- displays a list of choices for the user
- The SelectionMode property of the control
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
31Using a List Box in an Interface
32Adding 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 - Index
- a unique number that identifies an item in a
collection - is zero-relative the first item has index of 0
- Add method
- adds an item to the list boxs Items collection
- Sorted property
- Determines if the list box items are sorted
- Sort order is dictionary order
33Adding Items to a List Box
34Adding Items to a List Box
35Adding Items to a List Box
36SelectedItem 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
37SelectedItem and SelectedIndex Properties
38SelectedItem and SelectedIndex Properties
39SelectedValueChanged and SelectedIndexChanged
Events
- SelectedValueChanged or SelectedIndexChanged
events - occur when a user selects an item in a list box
40SelectedValueChanged and SelectedIndexChanged
Events
41Modifying the Monthly Payment Calculator
Application
42Modifying the Monthly Payment Calculator
Application (continued)
43Modifying the Monthly Payment Calculator
Application (continued)
44Using a Combo Box in an Interface
- ComboBox tool creates a combo box control
- ComboBox control
- Similar to a list box
- May contain a text field that allows the user to
type an entry that is not on the list - List portion may be hidden
- Three styles of combo boxes
- Simple
- DropDown
- DropDownList
45Using a Combo Box in an Interface (continued)
46Using a Combo Box in an Interface (continued)
47Using a Combo Box in an Interface (continued)
48Using a Combo Box in an Interface (continued)
- SelectedItem property contains the value of the
selected item in the list - Text property contains the value that appears in
the text portion of the control (item selected or
typed in)
49Using a Combo Box in an Interface (continued)
50Using a Combo Box in an Interface (continued)
51Using a Combo Box in an Interface (continued)
52Using a Combo Box in an Interface (continued)
53Programming Example
54Selecting Existing Text in a Text Box
- Windows standard highlight the existing text
when a text box receives the focus - SelectAll method selects all text in a text box
- Enter event occurs when the text box receives
the focus
55Selecting the Existing Text in a Text Box
(continued)
56Selecting the Existing Text in a Text Box
(continued)
57Selecting the Existing Text in a Text Box
(continued)
58Coding a Controls TextChanged Event Procedure
- TextChanged event
- Occurs when a change is made in a controls Text
property - Change may be made by user or the program
59Coding a Controls TextChanged Event Procedure
(continued)
60Coding a Controls TextChanged Event Procedure
(continued)