© 1999, by que education and training, appendix a, pages 735-744 of introduction to computer...

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 Combo Boxes & List Boxes List Box User selects from predefined set of textual choices Index number of the user’s selection is stored in ListIndex property There is no Text property! List property is used to show current list item selected ListCount property is equal to the number of items in the list box Combo Box User selects from predefined set of textual choices or enters own textual response Selected response is assigned to Text property 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

Upload: bertram-griffith

Post on 03-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

© 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 List Box

User selects from predefined set of textual choices

Index number of the user’s selection is stored in ListIndex property

There is no Text property!

List property is used to show current list item selected

ListCount property is equal to the number of items in the list box

Combo Box User selects from

predefined set of textual choices or enters own textual response

Selected response is assigned to Text property

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

Page 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

© 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 Design time

List property1)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

In code 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”)

Page 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

© 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 Design time

List property1)Highlight only the

individual item to be deleted

2)Press DELETE

3)Press ENTER or click anywhere else to complete the operation

In code RemoveItem method

Call lstEx.RemoveItem(0)– will remove first item from list

Call cboEx.RemoveItem(3)– will remove fourth item from

list Index values of list items is

updated

Page 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

© 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 Design time

List property1)Highlight entire selection

2) Press DELETE key to erase all items in list

In code 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

Page 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

© 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 ListIndex property identifies which list item is

selected (highlighted) Controlling list selection at design time or in

code Select first list item by assigning 0 to ListIndex Select another list item by assigning the

corresponding position number to ListIndex Deselect all items by assigning -1 to ListIndex

Page 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

© 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 Sorted property

Boolean value that represents whether list items are displayed in sorted order or not

What is we wanted to preserve the original order? ItemData property can be used to hold the original

order of the list lstName.ItemData(IndexLocation) = Value

Page 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

© 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 ExamplePrivate 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

Page 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

© 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 ExamplePrivate 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

Page 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

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

ListCount property stores the current number of items in a list

ListCount is always equal to 1 plus the highest ListIndex

TotalItems = lstEx.ListCount

Page 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

© 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 ListsDim 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?