Download presentation
Presentation is loading. Please wait.
Published byMorgan Holland Modified over 9 years ago
1
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 1
2
2 List Box Properties The total number of items in a list box is lstBox.Items.Count Note: Each item in lstBox is identified by an index number from 0 to lstBox.Items.Count – 1 The index number of the currently highlighted item is given by: lstBox.SelectedIndex 0-based indexing is very common. It applies to strings, arrays, listbox and combo box items collections, database field numbering, and more.
3
3 More List Box Properties lstBox.Items: list of items in the list box. The value of the item with an index of n is: lstBox.Items(n) The data type of the elements in the lstBox.Items() array is Object. To display the first element of lstBox.Items in a text box: txtBox.Text = CStr(lstBox.Items(0)) The value of the currently selected item as a string can be obtained as lstBox.Text txtBox.Text = lstBox.Text 0-based indexing. Convert item to string before assigning to textbox. No need for conversion…list box’s Text property is already a string
4
4 The Sorted Property Items can be placed into the list at design time or run time The Sorted property causes items in the list to be sorted automatically as strings We’ll experiment with this property to see it’s effects in the display when At design time you enter items and then set Sorted to true You set Sorted to true and then add items in the code Caution: Numbers in a sorted list box will not necessarily be in increasing numerical order
5
Example 6.3.1: Form 5 lstStates 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.
6
Example 6.3.1: Code & Output 6 This is what happens if lstLastTen.Sorted is set to False.
7
Example 6.3.1: Code & Output 7 Experiment: See what happens when you manipulate each ListBox’s Sorted property in design and in runtime. This is what happens if lstLastTen.Sorted is set to True.
8
Warning If you put items into a collection at design time (not run time), and you set the Sorted property to True (also in design time), then the items are permanently sorted. This is not true if one of these is done at run time. In that case, the sorted collection will revert back to its design state after the program terminates. 8
9
Example 6.3.2: Running 9 lstStates Find the first item in the list that starts with the two letters typed in.
10
Example 6.3.2: Code 10 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.
11
11 Flags A flag is a variable that keeps track of whether a certain situation has occurred. The data type most suited to flags is Boolean.
12
Searching an Unsorted List A flag is used to indicate whether or not the sought-after item has been found. The flag variable is initially set to False and then set to True if and when the item is found. 12
13
13 More About Flags When flagVar is a variable of Boolean type, the statements If flagVar = True Then and If flagVar = False Then can be replaced by If flagVar Then and If Not flagVar Then
14
14 More About Flags (continued) The statements Do While flagVar = True and Do While flagVar = False can be replaced by Do While flagVar and Do While Not flagVar
15
Example 6.3.2: Code 15 Flag initialization Set flag to indicate the item was found Testing for a match
16
Example 6.3.2: Code 16 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
17
Example 6.3.2: Code 17 The variable i is 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 i after the loop terminates?
18
Example: 6.3.3 18 txtGrades lstGrades User fills the lstGrades listbox
19
Example 6.3.3: Code 19 Two things happening in this procedure: 1.Calculating the average grade 2.Determining the highest grade
20
Example 6.3.3: Code 20 Accumulating a sum Calculating the average using the sum and the total number of items in the list box Initializing the accumulator (the sum variable is an accumulator)
21
Example 6.3.3: Code 21 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
22
Example 6.3.3: Output 22
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.