Title: Lists and Loops
1 Lists and Loops List boxes and combo boxes List
box - list of items - useful for a list which
does not change Combo box - list of items -
different styles - dropdown - simple
combo - dropdown style - useful for a list
which the user is allowed to modify There is no
Caption for most of these. The Name appears at
design-time, but not at run-time.
2Filling the list at design time Fig. 7.3, 7.4 -
in the Properties window, select List - enter
items and ltctrlgt ltreturngt - hit ltreturngt to
exit Filling the list at run time - use the
AddItem method Example lstDrinks.AddItem Tea
cboFood.AddItem txtMessage.Text Example P.
203 The Listindex property - each item in a
list has an index indexes start at 0 - when an
item is highlighted, then Listindex is set to
that value
3Example Consider the list Drinks List
Index Coffee 0 Tea 1 Milk 2 When the
user highlights Tea, the Listindex is set to 1.
To deselect all items, set Listindex to -1.
Example lstDrinks.ListIndex -1 Example P.
203 The Listcount property Listcount is the
number of elements in a list Example
lblMessage.Caption lstDrinks.ListCount will
print 3 on the form.
4Note ListCount equals (highest index in the
list) 1 The List Property Display an element
of a list Example To put Milk on the
form lstDrinks.ListIndex 2 lblMessage.Caption
lstDrinks.List (lstDrinks.ListIndex) Change an
element of a list Example To replace Tea with
Coke lstDrinks.List(1) Coke Example P. 204
51. Create a form with a list box and with each of
the three different styles of combo boxes. 2.
Enter a list for each box. 3. Write command
buttons to do the following to one of the
lists - add an item - display an item -
change an item
6For/Next Loops Definition A loop is a set of
instructions which is executed more than once.
Example P. 204 Dim iLoopIndex As Integer For
iLoopIndex 1 to lstSchools.ListCount do
something Next iLoopIndex Suppose that
ListCount is 3. condition iLoopIndex is
1 actions do something increment
iLoopIndex condition iLoopIndex is 2 actions do
something increment iLoopIndex condition
iLoopIndex is 3 actions do something
increment iLoopIndex condition iLoopIndex is
3 actions go to the first isntruction after
the loop
7Example Note that the example in the book has a
Null False branch. The flowchart should be drawn
as shown here.
8Dim iLoopIndex As Integer For iLoopIndex 1 to
lstSchools.ListCount do something Next
iLoopIndex Definition The variable iLoopIndex is
called the loop control variable, or counter.
The number 1 is the lower bound or initial value
of the loop control variable. The variable
lstSchool.Count holds the upper bound or
terminal value of the loop control variable.
Important Do NOT attempt to change the value of
the counter or the bounds within the loop. You
may print them only.
9 Altering the values of the loop control
variable NO!!!
10Exiting For/Next loops NO!!!
11Negative increment on counting loops Example
For iCount 10 to 1 Step -1 Next
iCount Infinite loops It is possible that you
write a loop which does not terminate. Example
For iCount 10 to 1 Next iCount In this
case, stop the execution of the program, start
the program again, and Step through the code.
Note If your program runs but nothing happens,
it may be in an infinite loop.
12Modify the last lab assignment. Write a loop to
print out all the elements of one of your lists.
Put each element into a separate label. Use a
loop control array for the labels. Sending
information to the printer (for creating
reports) The command is Printer.Print Formatting
lines - commas advance output one print area
- semicolons separate items without as much
space - trailing commas and semicolons print on
the same line - print a blank line
Printer.Print - the Tab function -
specify the position where you want output -
TAB (position)
13 - the Spc function - skip a number of
spaces - Spc (value) Selecting the font
Example Printer.Font.Name Times Printer.Fon
t.Size 24 Terminating the page Printer.NewPage
Terminating the job Printer.EndDoc