6 3 list boxes and loops l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
6.3 List Boxes and Loops PowerPoint Presentation
Download Presentation
6.3 List Boxes and Loops

Loading in 2 Seconds...

play fullscreen
1 / 22

6.3 List Boxes and Loops - PowerPoint PPT Presentation


  • 147 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '6.3 List Boxes and Loops' - oakley


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
6 3 list boxes and loops
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
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
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
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
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
Example 6.3.1: Code & Output

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

example 6 3 1 code output7
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
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
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
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
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
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
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
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 code15
Example 6.3.2: Code

Flag initialization

Testing for a match

Set flag to indicate the item was found

example 6 3 2 code16
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 code17
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
Example: 6.3.3

txtGrades

lstGrades

User fills the lstGrades listbox

example 6 3 3 code
Example 6.3.3: Code

Two things happening in this procedure:

Calculating the average grade

Determining the highest grade

example 6 3 3 code20
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 code21
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