Top Banner
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
22

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.

Jan 01, 2016

Download

Documents

Morgan Holland
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 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.

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

Page 2: 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.

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.SelectedIndex0-based indexing is very common. It applies to strings, arrays, listbox and combo box items collections, database field numbering, and more.

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

Example 6.3.1: Form

5

lstStateslstStates 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.

Page 6: 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.

Example 6.3.1: Code & Output

6

This is what happens if lstLastTen.Sorted is set to False.

Page 7: 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.

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.

Page 8: 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.

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

Page 9: 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.

Example 6.3.2: Running

9

lstStates

Find the first item in the list that starts with the two letters typed in.

Page 10: 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.

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.

Page 11: 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.

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.

Page 12: 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.

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

Page 13: 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.

13

More About FlagsWhen flagVar is a variable of Boolean type, the statements If flagVar = True Thenand If flagVar = False Thencan be replaced by If flagVar Then and If Not flagVar Then

Page 14: 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.

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

Page 15: 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.

Example 6.3.2: Code

15

Flag initialization

Set flag to indicate the item was found

Testing for a match

Page 16: 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.

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

Page 17: 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.

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?

Page 18: 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.

Example: 6.3.3

18

txtGrades

lstGrades

User fills the lstGrades listbox

Page 19: 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.

Example 6.3.3: Code

19

Two things happening in this procedure: 1.Calculating the average grade2.Determining the highest grade

Page 20: 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.

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)

Page 21: 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.

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

Page 22: 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.

Example 6.3.3: Output

22