Microsoft Visual Basic 2008: Reloaded Third Edition - PowerPoint PPT Presentation

View by Category
About This Presentation
Title:

Microsoft Visual Basic 2008: Reloaded Third Edition

Description:

Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Seven The For Next Loop and String Manipulation – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 65
Provided by: pbwo850
Learn more at: http://mrsrobb.pbworks.com
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Microsoft Visual Basic 2008: Reloaded Third Edition


1
Microsoft Visual Basic 2008 Reloaded Third
Edition
  • Chapter Seven
  • The ForNext Loop
  • and String Manipulation

2
Objectives
  • After studying this chapter, you should be able
    to
  • Include the ForNext loop in both pseudocode and
    a flowchart
  • Write a ForNext statement
  • Calculate a periodic payment using the
    Financial.Pmt method
  • Select the existing text in a text box
  • Code the TextChanged event procedure

2
3
Objectives (continued)
  • Include a combo box in an interface
  • Manipulate strings

3
4
The For...Next Statement
  • For...Next statement processes a set of
    instructions a known number of times
  • Is a pretest loop
  • Counter variable used to keep track of the
    number of times the loop has been processed
  • Startvalue, endvalue, and stepvalue items
  • Control the number of times the loop is processed
  • Must evaluate to numeric values
  • Can be positive or negative
  • A negative stepvalue causes the loop counter to
    count down

4
5
Figure 7-1 How to use the ForNext statement
5
6
The For...Next Statement (continued)
Figure 7-2 Processing tasks for the ForNext
statement
6
7
Figure 7-3 Processing steps for the code shown
in Example 1
7
8
The For...Next Statement (continued)
  • Flowchart symbol for the For...Next loop is a
    hexagon
  • Values for the counter variable, startvalue,
    stepvalue, and endvalue are shown within the
    hexagon

8
9
Figure 7-4 Pseudocode and flowchart for Example 1
9
10
The Financial.Pmt Method
  • Financial.Pmt method
  • Calculates a periodic payment on a loan or
    investment
  • Returns the periodic payment as a Double type
    value
  • Rate and number of periods arguments must be
    expressed in the same units (monthly, annual,
    etc.)
  • Also calculates the amount that must be saved
    each period to accumulate a specific sum

10
11
Figure 7-5 How to use the Financial.Pmt method
11
12
The Monthly Payment Calculator Application
Figure 7-5 Sample run of the Monthly Payment
Calculator application
12
13
Figure 7-7 Two procedures in the Monthly Payment
Calculator application
13
14
Figure 7-7 Two procedures in the Monthly Payment
Calculator application (continued)
14
15
Selecting the 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

15
16
Selecting the Existing Text in a Text Box
(continued)
Figure 7-8 How to select the existing text in a
text box
16
17
Figure 7-9 The principalTextBoxs Enter event
procedure
Figure 7-10 Result of processing the Enter event
procedure
17
18
Clearing the PaymentsLabel Control
  • TextChanged event
  • Occurs when a change is made in a controls Text
    property
  • Change may be made by user or the program

Figure 7-11 New principal entered in the
Principal text box
18
19
Figure 7-12 The principalTextBoxs TextChanged
event procedure
Figure 7-13 Status of the paymentsLabel after
entering a value in the text box
19
20
Clearing the PaymentsLabel Control (continued)
Figure 7-14 The termListBoxs SelectedValueChange
d event procedure
20
21
Using a Combo Box in an Interface
  • Combo Box control
  • Similar to a list box with a list of choices
  • 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 (the default)
  • DropDownList

21
22
Using a Combo Box in an Interface (continued)
Figure 7-15 Examples of the combo box styles
22
23
Using a Combo Box in an Interface (continued)
Figure 7-15 Code used to fill the combo boxes
with values
23
24
Using a Combo Box in an Interface (continued)
  • Items.Add method used to add items to a combo
    box
  • 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)
  • Items.count property used to obtain the number
    of items in the combo box
  • Sorted property used to sort the items in the
    list

24
25
Using a Combo Box in an Interface (continued)
Figure 7-17 Sample run of the Monthly Payment
Calculator application using a combo box
25
26
Using a Combo Box in an Interface (continued)
Figure 7-15a Code for the Monthly Payment
Calculator using a combo box
26
27
Figure 7-15b Code for the Monthly Payment
Calculator using a combo box
27
28
String Manipulation
  • Most applications need to manipulate string data
    in some fashion
  • String properties and methods are used to
    manipulate string data

28
29
Determining the Number of Characters Contained in
a String
Figure 7-19a How to use the Length property
29
30
Determining the Number of Characters Contained in
a String (continued)
Figure 7-19b How to use the Length property
30
31
Removing Characters from a String
  • TrimStart method removes one or more characters
    from the beginning of a string
  • TrimEnd method removes one or more characters
    from the end of a string
  • Trim method removes one or more characters from
    both the beginning and end of a string
  • Each of these methods returns a string with the
    appropriate characters removed
  • trimChars argument
  • Comma-separated list of characters to be removed
  • If omitted, spaces will be removed
  • Default value is the space character

31
32
Figure 7-20 How to use the TrimStart, TrimEnd,
and Trim methods
32
33
The Remove Method
  • Remove method
  • Removes characters from a string
  • Can remove one or more characters located
    anywhere in the string
  • Returns a string with the appropriate characters
    removed
  • startIndex argument
  • The position of a character in a string
  • Is zero-relative (starts with 0 as first
    position)
  • count argument number of characters to remove

33
34
The Remove Method (continued)
Figure 7-21 How to use the Remove method
34
35
Replacing Characters in a String
  • Replace method replaces a sequence of characters
    in a string with another sequence of characters
  • Arguments
  • string the original String value
  • oldValue sequence of characters you want to
    replace
  • newValue replacement characters

35
36
Figure 7-22 How to use the Replace method
36
37
The Mid Statement
  • Mid statement
  • Replaces a specified number of characters in a
    string with characters from another string
  • Arguments
  • targetString the string in which characters are
    to be replaced
  • replacementString the replacement characters
  • start the starting position for the replacement
  • count number of characters to replace

37
38
The Mid Statement (continued)
  • Character position of characters in the string
    starts with 1 (not the same as index)
  • Count argument is optional

38
39
Figure 7-23 How to use the Mid statement
39
40
Inserting Characters in a String
  • PadLeft method inserts characters at the
    beginning of a string
  • PadRight method inserts characters at the end of
    a string
  • Arguments
  • length represents the total length of the
    desired resulting string
  • character the character used to pad the string
    default value is the space character
  • string the string on which the operation takes
    place

40
41
Figure 7-24 How to use the PadLeft and PadRight
methods
41
42
The Insert Method
  • Insert method used to insert characters anywhere
    within a string
  • Arguments
  • startIndex specifies where in the string to
    insert the value
  • string the string being manipulated
  • value the character(s) to be inserted

42
43
Figure 7-25 How to use the Insert method
43
44
Searching a String
  • StartsWith method determines whether a specific
    sequence of characters occurs at the beginning of
    a string
  • EndsWith method determines whether a specific
    sequence of characters occurs at the end of a
    string
  • Arguments
  • subString sequence of characters to be searched
    for
  • StartsWith and EndsWith methods both return
    Boolean values and perform a case-sensitive search

44
45
Figure 7-26 How to use the StartsWith and
EndsWith methods
45
46
Searching a String (continued)
Figure 7-26 How to use the StartsWith and
EndsWith methods (continued)
46
47
The Contains Method
  • Contains method
  • Determines if a string contains a specific
    sequence of characters
  • Is a method of the String class
  • Returns a Boolean value
  • Performs a case-sensitive search
  • Arguments
  • subString represents the sequence of characters
    to be searched for
  • string the string in which to search

47
48
Figure 7-27 How to use the Contains method
48
49
The IndexOf Method
  • IndexOf method returns an integer representing
    the location of a substring within a string
  • Performs a case-sensitive search
  • Arguments
  • subString sequence of characters to be searched
    for
  • string the string to be searched
  • startIndex the starting position for the search
    (zero-relative)

49
50
Figure 7-28 How to use the IndexOf method
50
51
Accessing Characters Contained in a String
  • Substring method accesses any number of
    characters contained in a string
  • Arguments
  • startIndex index of the first character to be
    accessed (zero-relative)
  • string the string containing the characters to
    be accessed
  • count number of characters to be accessed

51
52
Figure 7-29 How to use the Substring method
52
53
Comparing Strings
  • String.Compare method compares two strings
  • Arguments
  • string1, string2 the two strings to be compared
  • ignoreCase a Boolean value indicating whether to
    perform a case-sensitive (False) or
    case-insensitive (True) search

53
54
Comparing Strings (continued)
  • String.Compare returns an integer result
  • Returns 0 when string1 equals string2
  • Returns 1 when string1 gt string2
  • Returns -1 when string1 lt string2
  • String.Compare uses word sort rules
  • Numbers are less than lowercase letters
  • Lowercase letters are less than uppercase letters

54
55
Figure 7-30 How to use the String.Compare method
55
56
The Like Operator
  • Like operator
  • Uses pattern-matching characters to determine if
    one string is equal to another
  • Returns a Boolean value (True/False)
  • Arguments
  • pattern contains one or more pattern-matching
    characters
  • string the string to be evaluated
  • charList a listing of characters to be matched

56
57
Figure 7-31 How to use the Like operator
57
58
Figure 7-31 How to use the Like operator
(continued)
58
59
Programming Tutorial
  • Creating the Hangman Game

Figure 7-33 User interface
59
60
Programming Example
  • Principal and Interest Calculator

Figure 7-46 User interface
60
61
Summary
  • For...Next statement a pretest loop that will
    process the instructions a fixed number of times
  • A variable declared in a For clause has block
    scope
  • Use a hexagon in a flowchart for a ForNext loop
  • Financial.Pmt method calculates a periodic
    payment on a loan or investment
  • SelectAll method highlights text in a text box
  • TextChanged event occurs when a controls text
    changes

61
62
Summary (continued)
  • Combo box similar to a list box but may not
    expose the list items until clicked
  • Three styles of combo boxes Simple, DropDown,
    and DropDownList
  • Combo boxs Items.Add method adds an item to the
    list
  • Use SelectedItem, SelectedIndex, or Text property
    of a combo box to set the default item in a combo
  • Combo boxs Items.Count property used to obtain
    the number of items listed in the combo box

62
63
Summary (continued)
  • Combo boxs Sorted property used to sort items
    listed in a combo box
  • Combo boxs SelectedItem property contains the
    value of the item selected in the list portion of
    the combo box
  • Combo boxs Text property contains the value
    that appears in the text portion of the combo box
  • Combo boxs TextChanged event occurs when the
    user either selects an item in the list portion
    or types a value in the text portion

63
64
Figure 7-50 String manipulation techniques
64
About PowerShow.com