1 / 22

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 . The total number of items in a list box is lstBox.Items.Count

oakley
Download Presentation

6.3 List Boxes and Loops

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  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. 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. 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 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 This is what happens if lstLastTen.Sorted is set to False.

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

  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.

  9. Example 6.3.2: Running lstStates Find the first item in the list that starts with the two letters typed in.

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

  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.

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

  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 Flag initialization Testing for a match Set flag to indicate the item was found

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

  17. 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?

  18. Example: 6.3.3 txtGrades lstGrades User fills the lstGrades listbox

  19. Example 6.3.3: Code Two things happening in this procedure: Calculating the average grade Determining the highest grade

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

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

  22. Example 6.3.3: Output

More Related