Microsoft Visual Basic 2005: Reloaded Second Edition - PowerPoint PPT Presentation

1 / 60
About This Presentation
Title:

Microsoft Visual Basic 2005: Reloaded Second Edition

Description:

The SelectionMode property of the control controls the ... SelectedItem and SelectedIndex Properties. Microsoft Visual Basic 2005: Reloaded, Second Edition ... – PowerPoint PPT presentation

Number of Views:107
Avg rating:3.0/5.0
Slides: 61
Provided by: csmast
Category:

less

Transcript and Presenter's Notes

Title: Microsoft Visual Basic 2005: Reloaded Second Edition


1
Microsoft Visual Basic 2005 Reloaded Second
Edition
  • Chapter 5
  • Repeating Program Instructions

2
Objectives
  • 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

3
We use loops because computers are really good at
doing the same thing over over over again...

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

5
Types 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

6
ForNext 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?

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

8
The For...Next Statement
A nice way to display results from each iteration
9
The 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

10
What 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

11
Horizontally
  • We must build the output string as we go
  • For num 1 To 5
  • evenNumsLabel.Text
  • evenNumsLabel.Text num.ToString
  • Next num

12
Vertically
  • 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?
13
All three loops in VB
  • While
  • entrance controlled - pretest
  • Until
  • exit controlled - posttest
  • For Next
  • Counting - pretest

14
How 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

15
But 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...

16
While 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!

17
Pretest
  • Dim Number As Integer
  • Number 1
  • Do While number lt 3
  • messageBox.Show (number.ToString)
  • number number 1
  • Loop

18
The pretest Do...Loop
19
Until 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!

20
Posttest
  • Dim Number As Integer
  • number 1
  • Do
  • messageBox.Show (number.ToString)
  • number number 1
  • Loop Until number gt 3

21
The posttest Do...Loop
22
When 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...

23
Examples
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?

24
How to check for correct password?
  • Which loop to use?
  • How many tries will we give the user?
  • First, lets do unlimited
  • Then three tries

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

26
The Sales Express Application
Use input box
Use input box
27
The 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

28
The InputBox Function
29
Loops and List Boxes
A match made in heaven!
A match made in heaven!
30
Using 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

31
Using a List Box in an Interface
32
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
  • 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

33
Adding Items to a List Box
34
Adding Items to a List Box
35
Adding Items to a List Box
36
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

37
SelectedItem and SelectedIndex Properties
38
SelectedItem and SelectedIndex Properties
39
SelectedValueChanged and SelectedIndexChanged
Events
  • SelectedValueChanged or SelectedIndexChanged
    events
  • occur when a user selects an item in a list box

40
SelectedValueChanged and SelectedIndexChanged
Events
41
Modifying the Monthly Payment Calculator
Application
42
Modifying the Monthly Payment Calculator
Application (continued)
43
Modifying the Monthly Payment Calculator
Application (continued)
44
Using 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

45
Using a Combo Box in an Interface (continued)
46
Using a Combo Box in an Interface (continued)
47
Using a Combo Box in an Interface (continued)
48
Using 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)

49
Using a Combo Box in an Interface (continued)
50
Using a Combo Box in an Interface (continued)
51
Using a Combo Box in an Interface (continued)
52
Using a Combo Box in an Interface (continued)
53
Programming Example
54
Selecting 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

55
Selecting the Existing Text in a Text Box
(continued)
56
Selecting the Existing Text in a Text Box
(continued)
57
Selecting the Existing Text in a Text Box
(continued)
58
Coding 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

59
Coding a Controls TextChanged Event Procedure
(continued)
60
Coding a Controls TextChanged Event Procedure
(continued)
Write a Comment
User Comments (0)
About PowerShow.com