Microsoft Visual Basic 2008: Reloaded Third Edition

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Programming with Microsoft Visual Basic th Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter 8: Manipulating Strings
Chapter 7: Sub and Function Procedures
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 5 new The Do…Loop Statement
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
1.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
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.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter Four The Selection Structure
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
6.3 List Boxes and Loops Some Properties, Methods, and Events of List Boxes List Boxes Populated with Strings List Boxes Populated with Numbers Searching.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
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.
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
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 8: Manipulating Strings
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
1.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
© 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.
Unit 6 Repetition Processing Instructor: Brent Presley.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
© 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,
© 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. 
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Clearly Visual Basic: Programming with Visual Basic nd Edition
The Selection Structure
Repeating Program Instructions
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 5 The Do…Loop Statement
Microsoft Visual Basic 2005: Reloaded Second Edition
Objectives After studying this chapter, you should be able to:
CIS16 Application Development and Programming using Visual Basic.net
CIS 16 Application Development Programming with Visual Basic
String Manipulation and More Controls
Introduction to Problem Solving and Control Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Presentation transcript:

Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Seven The For…Next Loop and String Manipulation

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

Objectives (continued) Include a combo box in an interface Manipulate strings Microsoft Visual Basic 2008: Reloaded, Third Edition 3

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

Figure 7-1: How to use the For…Next statement Microsoft Visual Basic 2008: Reloaded, Third Edition 5

The For...Next Statement (continued) Figure 7-2: Processing tasks for the For…Next statement Microsoft Visual Basic 2008: Reloaded, Third Edition 6

Figure 7-3: Processing steps for the code shown in Example 1 Microsoft Visual Basic 2008: Reloaded, Third Edition 7

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

Figure 7-4: Pseudocode and flowchart for Example 1 Microsoft Visual Basic 2008: Reloaded, Third Edition 9

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

Figure 7-5: How to use the Financial.Pmt method Microsoft Visual Basic 2008: Reloaded, Third Edition 11

The Monthly Payment Calculator Application Figure 7-5: Sample run of the Monthly Payment Calculator application Microsoft Visual Basic 2008: Reloaded, Third Edition 12

Figure 7-7: Two procedures in the Monthly Payment Calculator application Microsoft Visual Basic 2008: Reloaded, Third Edition 13

Figure 7-7: Two procedures in the Monthly Payment Calculator application (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 14

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

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

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

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

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

Clearing the PaymentsLabel Control (continued) Figure 7-14: The termListBox’s SelectedValueChanged event procedure Microsoft Visual Basic 2008: Reloaded, Third Edition 20

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

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

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

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

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

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

Figure 7-15b: Code for the Monthly Payment Calculator using a combo box Microsoft Visual Basic 2008: Reloaded, Third Edition 27

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

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

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

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

Figure 7-20: How to use the TrimStart, TrimEnd, and Trim methods Microsoft Visual Basic 2008: Reloaded, Third Edition 32

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

The Remove Method (continued) Figure 7-21: How to use the Remove method Microsoft Visual Basic 2008: Reloaded, Third Edition 34

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

Figure 7-22: How to use the Replace method Microsoft Visual Basic 2008: Reloaded, Third Edition 36

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

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

Figure 7-23: How to use the Mid statement Microsoft Visual Basic 2008: Reloaded, Third Edition 39

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

Figure 7-24: How to use the PadLeft and PadRight methods Microsoft Visual Basic 2008: Reloaded, Third Edition 41

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

Figure 7-25: How to use the Insert method Microsoft Visual Basic 2008: Reloaded, Third Edition 43

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

Figure 7-26: How to use the StartsWith and EndsWith methods Microsoft Visual Basic 2008: Reloaded, Third Edition 45

Searching a String (continued) Figure 7-26: How to use the StartsWith and EndsWith methods (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 46

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

Figure 7-27: How to use the Contains method Microsoft Visual Basic 2008: Reloaded, Third Edition 48

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

Figure 7-28: How to use the IndexOf method Microsoft Visual Basic 2008: Reloaded, Third Edition 50

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

Figure 7-29: How to use the Substring method Microsoft Visual Basic 2008: Reloaded, Third Edition 52

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

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

Figure 7-30: How to use the String.Compare method Microsoft Visual Basic 2008: Reloaded, Third Edition 55

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

Figure 7-31: How to use the Like operator Microsoft Visual Basic 2008: Reloaded, Third Edition 57

Figure 7-31: How to use the Like operator (continued) Microsoft Visual Basic 2008: Reloaded, Third Edition 58

Programming Tutorial Creating the Hangman Game Figure 7-33: User interface Microsoft Visual Basic 2008: Reloaded, Third Edition 59

Programming Example Principal and Interest Calculator Figure 7-46: User interface Microsoft Visual Basic 2008: Reloaded, Third Edition 60

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

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

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

Figure 7-50: String manipulation techniques Microsoft Visual Basic 2008: Reloaded, Third Edition 64