CIS 230, Visual Basic
Ch. 7 Notes, page 1
Objective #1: Create and use list boxes and combo boxes.
- To handle information within a VB form, you can use the list box and
combo box controls.
- There are three styles of combo boxes: simple combo boxes, dropdown
combo boxes, and dropdown lists. No doubt you have already seen
all three of these in commercial Windows applications.
- Combo boxes can provide choices to the user at Run time. Instead of simply displaying
information, a combo box allows the user to select one item.
- Combo boxes also have a Text property which displays on the screen for
2 of the 3 combo box styles.
- Use the prefix "cbo" when naming combo boxes.
- List boxes can be used to display information on a form.
- If a list box is too small to display all of its items, VB adds a scroll bar to the list
box.
- Use the prefix "lst" when naming a list box.
- Three events associated with list boxes and combo boxes are particularly useful. You
will learn how to use the Change, GotFocus, and LostFocus
events in a later chapter.
Objective #2: Enter items into list boxes using the Properties window and the
AddItem method.
- There are a couple of ways to input data into a list box.
- You can type the data for a list box or a combo box into its List property
during design time. Be sure to press Control + Enter between items.
- You can use the AddItem method during Run time in order to add items to
the List property. If the name of your list box is lstPlayers then the statement
lstPlayers.AddItem
"Mickey Mantle" would add an item to the end of the
list box. You can place an item in a specific position by specifying the position in a
statement like,
lstPlayers.AddItem "Mickey Mantle", 4 This method
would move the original items in positions 4 and beyond back one
place.
Objective #3: Determine which item in a list is selected.
- When a user selects an item in a list box, the index number (that is, position) of that
item is stored in the ListIndex property of the list box or a combo box.
If no list item is selected by the user, the ListIndex property is set to -1. You can then
display that item which was selected by the user somewhere else in your project, in a
label perhaps.
Objective #4: Use the ListCount property to determine the number of items in a
list.
- To display the number of items that are contained in a list box or a combo box, you can
use the ListCount property. The statement lblNumPlayers = lstPlayers.ListCount
would display the number of items in the list box lstPlayers in the label lblNumPlayers.
Objective #5: Display a selected item from a list.
- If you simply need to display an item from the list box that is in a specific position,
you can use the List property of the list box. For example, if "Yogi Berra" is
in the third position of the list box then the statement
lblCatcher = lstPlayers.List(3)
would place his name in the label
lblCatcher. Note that the index position of the string "Yogi Berra" is 2 since
the first item of a list box is stored in index position 0.
Objective #6: Differentiate among the available types of combo boxes.
- Unlike list boxes, combo boxes have a Style property which determines
which of three styles the combo box is to be:
- dropdown combo - allows the user to see the Text property and to select
an item from a dropdown list of individual items.
- simple combo box - does not allow the user to see the Text property and
only allows the user to enter text into the text box area at Run time.
- dropdown list - does not allow the user to see the Text property but
does allow the user to select from a dropdown list of individual items.
Objective #7: Use Do/Loops and For/Next statements to iterate
through a loop.
- Programmers make much use of repetition statements. One type of
repetition statement is a loop. There are two kinds of loops, indeterminate
loops and determinant loops. Do/Loops are
commonly used indeterminate loops in VB and For/Next loops are
determinant ones.
- Do/Loops are indeterminate because they repeat a sequence of statements until a given
condition is met. How many times the loop will iterate (i.e. repeat)
cannot be determined by the programmer at Design time. The condition is
specified in a control expression that is placed at the "top of the
loop" (i.e. on its first line) or at the "bottom of the loop" (i.e. on its
last line.) If the control expression is located at the top of the loop then the loop can
be called a pretest. Otherwise, it is a posttest. Pretests are also
called top-checking loops and posttests are bottom-checking
loops.
- One typical format of a pretest is:
Do Until (control expression)
statements
Loop
where the control expression is any condition that evaluates to True or False. If the the
expression is False, then the computer skips the remaining statements of the loop and the
program continues with the first statement after the loop. Any number of statements can be
placed (and should be indented) in the loop.
- A typical format of a posttest is:
Do
statements
Loop Until (control expression)
- In both examples above the keyword Until can be replaced by While
to change the logic of the algorithm.
- Boolean variables used as flags are very useful in indeterminate loop
control expressions. A Boolean variable can only hold the value of True or False.
By setting a Boolean variable blnDone to True, a programmer could exit the loop such as
blnDone = False
Do Until blnDone
statements
Loop
- Be careful not to write infinite (or endless) loops.
Indeterminate loops can be infinite if the control expression never causes the loop to
terminate.
- For/Next loops on the other hand are determinant loops. A programmer can tell how many
iterations a For loop will have at design time. A loop index (sometimes called a loop
variable) can be used to step through the loop a predetermined number of times.
- An example of a For/Next loop is:
For j = 1 To 10
statements
Next j
in which case the loop would iterate 10 times exactly. Note that j's final value will be
11.
- A For/Next loop can use of a step value. In the example,
For m = 1 To 100 Step 20
statements
Next m
The loop would iterate 5 times with m taking on the values 1, 21, 41, 61, and 81. While
m's final value would be 101, the loop would not iterate when m equals 101.
- Do not attempt to change the value of the loop index within the statements of the loop.
This is bad programming style and will cause errors.
- If you wish to break out of a loop in some situation, you may use an Exit For statement inside the
loop. This will cause the program control to move to the first line of the program outside
of the loop.
Objective #8: Use the MsgBox function to determine the button pressed by the
user.
- Message boxes can be used to display more than one button. In fact, you
can determine which button the user pressed.
- The syntax for using the MsgBox function is MsgBox(Prompt [, Buttons] [,Title])
where Prompt is the prompt string the the user reads in the message box. Buttons refers to
a combination of Visual Basic buttons that appear in the message box. Title refers to the
caption on the message box. The function returns an integer value between 1 and 7
indicating which button was pressed. See the charts on p. 262 for details of using the
MsgBox function.
Objective #9: Use the string functions Left, Right, and Mid to refer to part of
a string and use the Len function to count the number of characters in a string.
- There are a number of string functions that can be used in VB to format strings nicely
for output or display. For example, if a person enters their name as "John Doe",
you may wish at a later point in the program to display the name "Mr. Doe". You
could use string concatenation along with the Right function to do this.
- The Left, Right, and Mid string
functions each return a selected portion of a string.
- The Left function returns a specific number of characters from the left
end of a string variable, a string literal, or a text box.
- The general form of the Left function is Left(StringExpression, NumberOfCharacters)
- Examples:
- Left("John", 2) returns "Jo"
' note that "John" is a string literal
- Left(strName, 5) returns "Sally" if the string
variable strName = "Sally Fields"
- Left(txtEntry, 3) returns "113" if the Text property of the
textbox, txtEntry is "113,456"
- The Right function returns a specific number of characters from the
right end of a string expression.
- The general form of the Right function is Right(StringExpression, NumberOfCharacters)
- Examples:
- Right("John", 3) returns "ohn"
- Right(strName, 5) returns "ields" if the string variable
strName = "Sally Fields"
- Right(txtEntry, 3) returns "456" if the Text property of the
textbox, txtEntry is "113,456"
- The Mid function returns a substring that begins with a particular
character of the original string and ends with a specified spot.
- The general form of the Mid function is Mid(String Expression, StartPosition,
NumberOfCharacters). The third argument NumberOfCharacters is optional. If it is not
specified then the whole substring formed by the characters after the StartPosition is
returned.
- Examples:
- Mid("John", 3, 1) returns the letter "h"
- Mid(strName, 5, 3) returns " Fi" (a total of 3 characters
with the first one being a space) if the string variable strName = "Sally
Fields"
- Mid(txtEntry, 3) returns "3,456"
- The Len function returns the length of a string expression as an
integer. All of the characters in the string expression are counted as part of the length
including spaces and punctuation symbols.
- Examples:
- Length("Mississippi") = 11
- Length(strName) = 12 where strName = "Sally Fields"
- Length("Bill" & "Gates") = 9
- Length(Right("Visual", 3) & "Basic") = 8
- The SelStart and SelLength properties of text boxes
can be useful with the string formatting functions. When the focus is placed into a text
box, the user can select any number of characters in the string expression of its Text
property. The SelStart property of a text box can set or return the position of the first
selected character. The SelLength property can set or return the number of selected
characters.
- Examples:
- This event subroutine causes the text within the text box named txtExample to be
selected as soon as it receives the focus:
Private Sub txtExample_GotFocus( )
With txtExample
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
Objective #10: Send information to the printer using the Print method.
- The Print method allows you to print text on a form, on the Printer
object, or in the Debug window.
- Each time that you use the Printer.Print method, your output is added
to the Printer object which is created by VB. When the print job ends or when it receives
an EndDoc or NewPage method, VB sends the current
contents of the Printer object to the printer.
- The NewPage method actually sends the current page to the printer and clears the printer
object in the memory of the computer so you can begin a new page. The EndDoc method sends
the current page to the printer and terminates the whole printer job. Within code the
statements would look like Printer.NewPage
and Printer.EndDoc since they are
methods.
Objective #11: Control the format of printing using commas,
semicolons, the Tab function, and the Spc function.
- Choose a fixed-pitch font such as Courier, if you want every character of your printed
output to take the same amount of space. Otherwise, you may be using a proportional font.
- A VB output page has preset tab settings with five columns per line. Each column is a print
zone. A comma placed in a string of output is used to advance a
single print zone. Therefore a comma works similarly to a tab in word-processing or
typing.
- Example: Printer.Print "Name",
"Social Security Number" causes Name to appear in
the first print zone and Social Security Number in the second print zone.
- The only way to change the size of a print zone is to change the font size. The width of
a print zone is 14 characters, based on the average size of a character for the font being
used. So more than 14 characters could fit in a print zone if you used a proportional font
and included many i's and t's. Sometimes, the items you print could overflow the print
zone. If the number of print zones exceeds the number for a single line, the output will
word wrap to the next line on the printed page. To change the Font property of the Printer
object, you must use statements like Printer.Font.Name
= "Arial" and Printer.Font.Size = 12
- If you wish two string expressions to be printed next to each other you can use a semicolon
in the output. For example, Printer.Print
"Vis";"ual" will cause the string
"Visual" to be printed on the printer.
- To print a blank line, simply use the statement Printer.Print
- The Tab function can be used to specify the column position in which
you wish to precisely print a string expression. For example, the statement
Printer.Print Tab(10);
"Hello" prints the string
hello where the 'H' in the 10th column of the line.
- The Spc function can be used to specify a number of spaces between
several printed items. For example, the statement
Printer.Print "John";Spc(10);"Doe" causes
10 blank spaces to appear between "John" and "Doe".
- It is possible to print string expressions next to each other with statements like
Printer.Print strFirst & strLast
which would print
"JohnDoe" if strFirst = "John" and strLast = "Doe". But VB
automatically adds a space in front of a numerical value for the sign (positive or
negative). Therefore, the statement Printer.Print
"123" & 456 would print
"123 456" since space is allowed for the sign of the numerical value 456.
CIS 230 Home Page | Mr. Minich's Education Home Page | Minich.com Web Design