1 / 25

Chapter 6 – Repetition

Chapter 6 – Repetition. 6.1 Do Loops 6.2 For...Next Loops 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 Unordered Lists Ordered Lists.

thai
Download Presentation

Chapter 6 – Repetition

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. Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops

  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 • Unordered Lists • Ordered Lists

  3. # of Items in a Listbox • The total number of items in a list box is: listBox.Items.Count • Each item in listBox is identified by an index number from: 0 to listBox.Items.Count – 1

  4. Accessing ListBox Items • listBox.Items(i) is used to access the value of an item in a list box (where i is the index number) • The index identifies the relative position of items in the list. • The first item’s index number is 0 • The second item’s index number is 1 • The last item’s index number is listbox.items.count - 1 • The first element of listBox.Items can be displayed in a text box as follows: txtBox.Text = CStr(listBox.Items(0)) • The last element of listBox.Items can be displayed in a text box as follows: n = listbox.items.Count - 1txtBox.Text = CStr(listBox.Items(n))

  5. Anatomy of a Listbox What does following statement do? class = cstr(listbox1.items(2)) What does following statement do? n = listbox1.items.count What does following statement do? Textbox.text = listbox1.items (listbox1.items.count – 1)

  6. Object Data Type • The Objectdata type is the parent of all data types. • The data type of the elements in the listBox.Items(i) array are Object. • You may have noticed we used the convert each item to string in the list box. txtBox.Text = CStr(lstBox.Items(0))

  7. Stepping Through EachItem in a ListBox The following code copies the items from one list box to a second list box. For i As Integer = 0 To ListBox1.Items.Count – 1 ListBox2.Items.Add(CStr(ListBox1.Items(i))) Next

  8. The Sorted Property • Items can be placed into the list at design time or run time • The Sortedproperty causes items in the list to be sorted automatically as strings • Caution: Numbers in a sorted list box will not necessarily be in increasing numerical order

  9. Copy and Sort This code is the sameas the previous code,but now Listbox2’s sorted property is True. For i As Integer = 0 To ListBox1.Items.Count – 1 ListBox2.Items.Add(CStr(ListBox1.Items(i))) Next

  10. SelectedIndex The index numberof the currently highlighted itemis given by: ListBox2.Items.Add(ListBox1.SelectedIndex)

  11. Currently Highlighted Item in a List Boxes The value of the currently highlighted item as a string can be obtained with thefollowing: ListBox2.Items.Add(ListBox1.Text)

  12. States Join the Union: Form In this program we will list the last 10 states to join the union. lstStates Copy from thetext file included on drive P: lstStates is filled at design time with the names of the U.S. states in the order they joined the union.

  13. States Join the Union: Code Private Sub btnDisplay_Click(...) Handles _ btnDisplay.Click 'Display last ten states to join the union Dim n As Integer = lstStates.Items.Count For i As Integer = (n - 1) To (n - 10) Step -1 lstLastTen.Items.Add(lstStates.Items(i)) Next End Sub n-1 is the index of the last item in the list

  14. States Join the Union: Output

  15. Average Age of Presidents: Form lstAges txtAvgAge lstAges is filled at design time with the ages of the U.S. presidents when taking office.

  16. Average Age of Presidents: Code Private Sub btnCalculate_Click(...) Handles _ btnCalculate.Click 'Calculate average age of presidents when assuming office Dim n As Integer = lstAges.Items.Count Dim sum As Double = 0 For i As Integer = 0 To n – 1 sum += CDbl(lstAges.Items(i)) Next txtAvgAge.Text = FormatNumber(sum / n, 2) End Sub

  17. Average Age of Presidents: Output

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

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

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

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

  22. Searching an Unordered List Private Sub Button1_Click(…) Handles Button1.Click Dim key As String, found As Boolean = False Dim i As Integer = 0 key = TextBox1.Text Do While Not found And i <= ListBox1.Items.Count - 1 If key = CStr(ListBox1.Items(i)) Then found = True End If i = i + 1 Loop If found Then ListBox2.Items.Add(“Found at index # " & (i - 1)) Else ListBox2.Items.Add("The item is not in the list.") End If End Sub Initialize It may be easier to think of this as: Do … Loop Until found or i > # items Once the value is located set found to true

  23. Searching a List of Strings • A searchoften can be terminated early if the list is sorted. • This is particularly the case when the sought-after item is not in the list. The search can be terminated when an item is reached that follows the sought-after item alphabetically.

  24. Search With Early Exit Private Sub Button1_Click(…) Handles Button1.Click Dim key As String Dim found As Boolean = False, doneEarly As Boolean = False Dim i As Integer = 0 key = TextBox1.Text Do While Not found And i <= ListBox1.Items.Count - 1 AndNot doneEarly If key = CStr(ListBox1.Items(i)) Then found = True End If Ifkey < CStr(ListBox1.Items(i)) Then doneEarly = True End If i = i + 1 Loop

  25. Search With Early Exit (Cont) If found Then ListBox2.Items.Add("The item you search for is at index # " & (i - 1)) Else If doneEarly Then ListBox2.Items.Add("Not in the List - left early") Else ListBox2.Items.Add("Not in the List") End If End If End Sub

More Related