220 likes | 431 Views
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
E N D
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 • 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.
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
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
Example 6.3.1: Form 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.
Example 6.3.1: Code & Output This is what happens if lstLastTen.Sorted is set to False.
Example 6.3.1: Code & Output 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.
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.
Example 6.3.2: Running lstStates Find the first item in the list that starts with the two letters typed in.
Example 6.3.2: Code 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.
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.
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.
More About Flags When flagVaris a variable of Boolean type, the statements IfflagVar = True Then and IfflagVar = False Then can be replaced by IfflagVarThen and If NotflagVarThen
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
Example 6.3.2: Code Flag initialization Testing for a match Set flag to indicate the item was found
Example 6.3.2: Code 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
Example 6.3.2: Code 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?
Example: 6.3.3 txtGrades lstGrades User fills the lstGrades listbox
Example 6.3.3: Code Two things happening in this procedure: Calculating the average grade Determining the highest grade
Example 6.3.3: Code 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
Example 6.3.3: Code 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