Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.

Similar presentations


Presentation on theme: "© 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach."— Presentation transcript:

1 © 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Combo Boxes & List Boxes n List Box i User selects from predefined set of textual choices i Index number of the user’s selection is stored in ListIndex property i There is no Text property! i List property is used to show current list item selected i ListCount property is equal to the number of items in the list box n Combo Box i User selects from predefined set of textual choices or enters own textual response i Selected response is assigned to Text property i Style property determines whether list will also have textbox or not as well as whether list will drop down or not Each item in list has an index value, starting at 0 for the first item

2 © 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Filling a list n Design time i List property 1)Type entry 2)Press Ctrl-Enter 3)Type next entry 4)Repeat 2 & 3 above until last entry has been added 5)Press ENTER or click anywhere else to complete the operation n In code i AddItem method Call lstEx.AddItem(“Apple”) Call lstEx.AddItem(“Banana”) Call lstEx.AddItem(“Carrot”) Call lstEx.AddItem(“Donut”) Call cboEx.AddItem(“Ape”) Call cboEx.AddItem(“Bear”) Call cboEx.AddItem(“Cat”)

3 © 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Removing individual list items n Design time i List property 1)Highlight only the individual item to be deleted 2)Press DELETE 3)Press ENTER or click anywhere else to complete the operation n In code i RemoveItem method u Call lstEx.RemoveItem(0) –will remove first item from list u Call cboEx.RemoveItem(3) –will remove fourth item from list u Index values of list items is updated

4 © 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Clearing a list n Design time i List property 1)Highlight entire selection 2) Press DELETE key to erase all items in list n In code i Clear method Assume lstEx has four items (Apple, Banana, Carrot, and Donut), and cboEx has three items (Ape, Bear, Cat) Call lstEx.Clear Call cboEx.Clear will erase all contents of the list box lstEx and the combo box cboEx

5 © 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Selecting/deselecting from lists n ListIndex property identifies which list item is selected (highlighted) n Controlling list selection at design time or in code i Select first list item by assigning 0 to ListIndex i Select another list item by assigning the corresponding position number to ListIndex i Deselect all items by assigning -1 to ListIndex

6 © 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Sorting lists n Sorted property i Boolean value that represents whether list items are displayed in sorted order or not n What is we wanted to preserve the original order? i ItemData property can be used to hold the original order of the list i lstName.ItemData(IndexLocation) = Value

7 © 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach List Example Private Sub cmdAdd_Click() Call lstEx.AddItem(txtEx.Text & vbTab & _ Format(lstEx.ListCount, "00")) lstEx.ItemData(lstEx.NewIndex) = lstEx.ListCount - 1 Call lstEx2.AddItem(txtEx.Text & vbTab & _ Format(lstEx2.ListCount, "00")) lstEx2.ItemData(lstEx2.NewIndex) = lstEx2.ListCount - 1 txtEx.SelStart = 0 txtEx.SelLength = Len(txtEx.Text) End Sub Private Sub cmdOriginal_Click() lstEx.Visible = False lstEx2.Visible = True lstEx3.Visible = False End Sub Private Sub cmdSort_Click() lstEx.Visible = True lstEx2.Visible = False lstEx3.Visible = False End Sub

8 © 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach List Example Private Sub cmdItemData_Click() Dim ListLoc As Integer Call lstEx3.Clear For ListLoc = 0 To lstEx2.ListCount - 1 Step 1 Call lstEx3.AddItem(lstEx2.List(lstEx2.ItemData(ListLoc))) Next ListLoc lstEx.Visible = False lstEx2.Visible = False lstEx3.Visible = True End Sub

9 © 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Counting items in list n Sometimes the number of list items is not determined at design time, but the program needs to know the total number of items in a list. n ListCount property stores the current number of items in a list n ListCount is always equal to 1 plus the highest ListIndex n TotalItems = lstEx.ListCount

10 © 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach For-Next Loops for Lists Dim LCount As Integer For LCount = 0 To (lstEx.ListCount-1) Step 1 Printer.Print lstEx.List(LCount) Next LCount steps to process after loop ends False True Steps to process after loop ends Print current list item LCount > location of last list item? LCount = 0 LCount = LCount + 1 What changes are needed to print the list items in reverse order? What processing is needed to find the total of list items? The minimum? The maximum?


Download ppt "© 1999, by Que Education and Training, Appendix A, pages 735-744 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach."

Similar presentations


Ads by Google