Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
-
Upload
cory-flowers -
Category
Documents
-
view
221 -
download
1
Transcript of Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
![Page 1: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/1.jpg)
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Chapter TenString Manipulation and Menus
![Page 2: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/2.jpg)
Objectives
After studying this chapter, you should be able to:
• Determine the number of characters in a string
• Remove characters from a string
• Insert characters in a string
• Search a string
• Access the characters in a string
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
![Page 3: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/3.jpg)
Objectives (cont’d.)
• Align the characters in a string
• Compare strings using pattern-matching
• Add a menu to a form
• Code a menu item’s Click event procedure
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
![Page 4: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/4.jpg)
Working with Strings
• Most applications need to manipulate string data in some way
• String properties and methods are used to manipulate string data
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4
![Page 5: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/5.jpg)
Determining the Number of Characters in a String
• Length property: stores the number of characters contained in a string as an integer value– Can be used with a String variable, a String named
constant, or the Text property of a control
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5
![Page 6: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/6.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 10-1: How to determine the number of characters in a string6
![Page 7: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/7.jpg)
Removing Characters from a String
• Computer first makes a temporary copy of the string in memory and operates on the copy only
• Trim method: removes one or more spaces from both the beginning and end of a string
• Remove method: removes a specified number of characters located anywhere in a string
• Index: an integer indicating the character’s position in the string– The first character in a string has an index of 0
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7
![Page 8: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/8.jpg)
Removing Characters from a String (cont’d.)
• Arguments:– startIndex argument: the index of the first character
to be removed– numCharsToRemove argument: number of
characters to be removed• If omitted, all characters from the startIndex position
through the end of the string are removed
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8
![Page 9: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/9.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 10-2: How to remove characters from a string
9
Removing Characters from a String (cont’d.)
![Page 10: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/10.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 10-2: How to remove characters from a string (cont’d.)
10
Removing Characters from a String (cont’d.)
![Page 11: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/11.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Inserting Characters in a String
• Insert method: used to insert characters anywhere within a string
• Computer makes and operates on a temporary copy of the string
• Arguments:– startIndex: specifies where in the string to insert the
value– value: the character(s) to be inserted
11
![Page 12: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/12.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12
Figure 10-3: How to insert characters in a string
Inserting Characters in a String (cont’d.)
![Page 13: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/13.jpg)
Searching a String
• Contains method: – Determines if a string contains a specific sequence
of characters– Returns a Boolean value of True when the substring
is contained in the string, and False if not– Performs a case-sensitive search
• Arguments:– subString: represents the sequence of characters to
be searched for
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13
![Page 14: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/14.jpg)
Searching a String (cont’d.)
• 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– startIndex: the starting position for the search (zero-
relative)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 14
![Page 15: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/15.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15
Figure 10-4: How to search a string
![Page 16: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/16.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 16
Figure 10-4: How to search a string (cont’d.)
![Page 17: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/17.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Accessing the Characters 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)– numCharsToAccess: number of characters to be
accessed
17
![Page 18: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/18.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 18
Figure 10-5: How to access characters in a string
Accessing the Characters in a String (cont’d.)
![Page 19: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/19.jpg)
Aligning the 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:– totalChars: represents the total number of characters
you want in the resulting string– padCharacter: the character used to pad the string;
default value is the space character
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 19
![Page 20: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/20.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 20
Figure 10-6: How to align the characters in a string
![Page 21: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/21.jpg)
Using Pattern-Matching to Compare Strings
• 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– characterList: a listing of characters to be matched
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 21
![Page 22: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/22.jpg)
Using Pattern-Matching to Compare Strings
• Pattern-matching characters:– ? Represents 1 character only– * represents 0 or more characters– # represents a single digit
• Use square brackets [ ] to provide a list of characters to match– Use a hyphen between characters to specify a range
of characters
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 22
![Page 23: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/23.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23
Figure 10-7: How to use pattern-matching to compare strings
![Page 24: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/24.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24
Figure 10-7: How to use pattern-matching to compare strings (cont'd.)
![Page 25: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/25.jpg)
Adding a Menu to a Form
• Menu strip control: used to include one or more menus on a Windows form– Found in the Menus & Toolbars section of the
toolbox
• Menu title: appears on the menu bar at the top of the form– When clicked, the menu opens and displays a list of
options called menu items
• Clicking a menu item executes the command associated with it
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 25
![Page 26: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/26.jpg)
Adding a Menu to a Form (cont’d.)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Figure 10-8: Location of menu elements
26
![Page 27: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/27.jpg)
Adding a Menu to a Form (cont’d.)
• Each menu element is considered an object– Each has a set of properties associated with it
• Name property: used to refer to the menu element in code
• Text property: stores the menu element’s caption – the text the user sees
• Access key: used in combination with the Alt key, will open the menu
• Shortcut key: allows the user to select the item without opening the menu
Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27
![Page 28: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/28.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Adding a Menu to a Form (cont’d.)
28
Figure 10-9: Game menu
Figure 10-10: Exit command’s Click event procedure
![Page 29: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/29.jpg)
• Creating the Guess the Word Game Application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Programming Tutorial 1
29
Figure 10-12: MainForm in the Guess the Word Game application
![Page 30: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/30.jpg)
• Creating the Bucky Burgers Application
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Programming Tutorial 2
30
Figure 10-22: MainForm for the Bucky Burgers application
![Page 31: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/31.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Programming Example
31
• Yolanda Drapery Application
Figure 10-28: MainForm in the Yolanda Drapery application
![Page 32: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/32.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Summary
• Use a menu strip control to add one or more menus to a form
• Menu elements should have access keys
• String manipulation techniques:– Length property: number of characters in the string– Trim method: removes leading and trailing spaces– Remove method: removes characters from a string– Insert method: inserts characters into a string
32
![Page 33: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/33.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Summary (cont'd.)
• String manipulation techniques (cont’d.):– Contains method: determines whether a specific
sequence of characters appears in a string; returns a Boolean value
– IndexOf method: determines whether a specific sequence of characters appears in a string; returns the integer position where the sequence starts
– Substring method: accesses one or more characters in a string
33
![Page 34: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649dd95503460f94aceea9/html5/thumbnails/34.jpg)
Microsoft Visual Basic 2010: Reloaded, Fourth Edition
Summary (cont'd.)
• String manipulation techniques (cont’d.):– PadLeft method: pads the beginning of a string with
the specified character– PadRight method: pads the end of a string with the
specified character– Like operator: uses pattern-matching to compare
strings
34