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 an Ordered List. List Box Properties . The total number of items in a list box is lstBox.Items.Count
0-based indexing is very common. It applies to strings, arrays, listbox and combo box items collections, database field numbering, and more.
txtBox.Text = CStr(lstBox.Items(0))
txtBox.Text = lstBox.Text
Convert item to string before assigning to textbox.
No need for conversion…list box’s Text property is already a string
lstStates is filled at design time with the names of the U.S. states in the order they joined the union.
If a listbox is not big enough to display all the items in its collection, a scrollbar will appear.
This is what happens if lstLastTen.Sorted is set to False.
This is what happens if lstLastTen.Sorted is set to True.
Experiment: See what happens when you manipulate each ListBox’s Sorted property in design and in runtime.
Find the first item in the list that starts with the two letters typed in.
This does a Linear Search. For unsorted lists, a search needs to begin at the front of the collection and continue until the end, or a match is found.
When flagVaris a variable of Boolean type, the statements
IfflagVar = True Then
IfflagVar = False Then
can be replaced by
Do While flagVar = True
Do While flagVar = False
can be replaced by
Do While flagVar
Do While Not flagVar
Testing for a match
Set flag to indicate the item was found
Decision on when to quit the loop. Either we found the desired item, or we went through the entire collection
Using result to decide what to display
The variable iis used to index into the list box’s Items collection
Question: assume that the list box has 5 items, and we never find a match. What will be the value of iafter the loop terminates?
User fills the lstGrades listbox
Two things happening in this procedure:
Calculating the average grade
Determining the highest grade
Initializing the accumulator (the sum variable is an accumulator)
Accumulating a sum
Calculating the average using the sum and the total number of items in the list box
Setting an initial value for maxGrade
Replacing maxGrade if necessary…maxGrade will get progressively bigger.
After the loop, maxGrade contains the highest value from the list