Download presentation
Presentation is loading. Please wait.
Published byDominick Townsend Modified over 6 years ago
1
Microsoft Visual Basic 2008: Reloaded Third Edition
Chapter Seven The For…Next Loop and String Manipulation
2
Objectives After studying this chapter, you should be able to:
Include the For…Next loop in both pseudocode and a flowchart Write a For…Next statement Calculate a periodic payment using the Financial.Pmt method Select the existing text in a text box Code the TextChanged event procedure Microsoft Visual Basic 2008: Reloaded, Third Edition 2
3
Objectives (continued)
Include a combo box in an interface Manipulate strings Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 4
5
Figure 7-1: How to use the For…Next statement
Microsoft Visual Basic 2008: Reloaded, Third Edition 5
6
The For...Next Statement (continued)
Figure 7-2: Processing tasks for the For…Next statement Microsoft Visual Basic 2008: Reloaded, Third Edition 6
7
Figure 7-3: Processing steps for the code shown in Example 1
Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 8
9
Figure 7-4: Pseudocode and flowchart for Example 1
Microsoft Visual Basic 2008: Reloaded, Third Edition 9
10
The 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 10
11
Figure 7-5: How to use the Financial.Pmt method
Microsoft Visual Basic 2008: Reloaded, Third Edition 11
12
The Monthly Payment Calculator Application
Figure 7-5: Sample run of the Monthly Payment Calculator application Microsoft Visual Basic 2008: Reloaded, Third Edition 12
13
Figure 7-7: Two procedures in the Monthly Payment Calculator application
Microsoft Visual Basic 2008: Reloaded, Third Edition 13
14
Figure 7-7: Two procedures in the Monthly Payment Calculator application (continued)
Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 15
16
Selecting the Existing Text in a Text Box (continued)
Figure 7-8: How to select the existing text in a text box Microsoft Visual Basic 2008: Reloaded, Third Edition 16
17
Figure 7-9: The principalTextBox’s Enter event procedure
Figure 7-10: Result of processing the Enter event procedure Microsoft Visual Basic 2008: Reloaded, Third Edition 17
18
Clearing the PaymentsLabel Control
TextChanged event Occurs when a change is made in a control’s Text property Change may be made by user or the program Figure 7-11: New principal entered in the Principal text box Microsoft Visual Basic 2008: Reloaded, Third Edition 18
19
Figure 7-12: The principalTextBox’s TextChanged event procedure
Figure 7-13: Status of the paymentsLabel after entering a value in the text box Microsoft Visual Basic 2008: Reloaded, Third Edition 19
20
Clearing the PaymentsLabel Control (continued)
Figure 7-14: The termListBox’s SelectedValueChanged event procedure Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 21
22
Using a Combo Box in an Interface (continued)
Figure 7-15: Examples of the combo box styles Microsoft Visual Basic 2008: Reloaded, Third Edition 22
23
Using a Combo Box in an Interface (continued)
Figure 7-15: Code used to fill the combo boxes with values Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 25
26
Using a Combo Box in an Interface (continued)
Figure 7-15a: Code for the Monthly Payment Calculator using a combo box Microsoft Visual Basic 2008: Reloaded, Third Edition 26
27
Figure 7-15b: Code for the Monthly Payment Calculator using a combo box
Microsoft Visual Basic 2008: Reloaded, Third Edition 27
28
String Manipulation Most applications need to manipulate string data in some fashion String properties and methods are used to manipulate string data Microsoft Visual Basic 2008: Reloaded, Third Edition 28
29
Determining the Number of Characters Contained in a String
Figure 7-19a: How to use the Length property Microsoft Visual Basic 2008: Reloaded, Third Edition 29
30
Determining the Number of Characters Contained in a String (continued)
Figure 7-19b: How to use the Length property Microsoft Visual Basic 2008: Reloaded, Third Edition 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: “ ” Microsoft Visual Basic 2008: Reloaded, Third Edition 31
32
Figure 7-20: How to use the TrimStart, TrimEnd, and Trim methods
Microsoft Visual Basic 2008: Reloaded, Third Edition 32
33
The Remove Method Remove method: startIndex argument:
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 Microsoft Visual Basic 2008: Reloaded, Third Edition 33
34
The Remove Method (continued)
Figure 7-21: How to use the Remove method Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 35
36
Figure 7-22: How to use the Replace method
Microsoft Visual Basic 2008: Reloaded, Third Edition 36
37
The Mid Statement Mid statement: Arguments:
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 Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 38
39
Figure 7-23: How to use the Mid statement
Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 40
41
Figure 7-24: How to use the PadLeft and PadRight methods
Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 42
43
Figure 7-25: How to use the Insert method
Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 44
45
Figure 7-26: How to use the StartsWith and EndsWith methods
Microsoft Visual Basic 2008: Reloaded, Third Edition 45
46
Searching a String (continued)
Figure 7-26: How to use the StartsWith and EndsWith methods (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 46
47
The Contains Method Contains method: Arguments:
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 Microsoft Visual Basic 2008: Reloaded, Third Edition 47
48
Figure 7-27: How to use the Contains method
Microsoft Visual Basic 2008: Reloaded, Third Edition 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) Microsoft Visual Basic 2008: Reloaded, Third Edition 49
50
Figure 7-28: How to use the IndexOf method
Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 51
52
Figure 7-29: How to use the Substring method
Microsoft Visual Basic 2008: Reloaded, Third Edition 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 Microsoft Visual Basic 2008: Reloaded, Third Edition 53
54
Comparing Strings (continued)
String.Compare returns an integer result: Returns 0 when string1 equals string2 Returns 1 when string1 > string2 Returns -1 when string1 < string2 String.Compare uses word sort rules: Numbers are less than lowercase letters Lowercase letters are less than uppercase letters Microsoft Visual Basic 2008: Reloaded, Third Edition 54
55
Figure 7-30: How to use the String.Compare method
Microsoft Visual Basic 2008: Reloaded, Third Edition 55
56
The Like Operator Like operator: Arguments:
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 Microsoft Visual Basic 2008: Reloaded, Third Edition 56
57
Figure 7-31: How to use the Like operator
Microsoft Visual Basic 2008: Reloaded, Third Edition 57
58
Figure 7-31: How to use the Like operator (continued)
Microsoft Visual Basic 2008: Reloaded, Third Edition 58
59
Programming Tutorial Creating the Hangman Game
Figure 7-33: User interface Microsoft Visual Basic 2008: Reloaded, Third Edition 59
60
Programming Example Principal and Interest Calculator
Figure 7-46: User interface Microsoft Visual Basic 2008: Reloaded, Third Edition 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 For…Next 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 control’s text changes Microsoft Visual Basic 2008: Reloaded, Third Edition 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 box’s 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 box’s Items.Count property: used to obtain the number of items listed in the combo box Microsoft Visual Basic 2008: Reloaded, Third Edition 62
63
Summary (continued) Combo box’s Sorted property: used to sort items listed in a combo box Combo box’s SelectedItem property: contains the value of the item selected in the list portion of the combo box Combo box’s Text property: contains the value that appears in the text portion of the combo box Combo box’s TextChanged event: occurs when the user either selects an item in the list portion or types a value in the text portion Microsoft Visual Basic 2008: Reloaded, Third Edition 63
64
Figure 7-50: String manipulation techniques
Microsoft Visual Basic 2008: Reloaded, Third Edition 64
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.