|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Removing Items From A List BoxThe Testing Department is calling againhow about letting the users customize your program? You ask, what do you mean? Well, they say, lets give the user some way of removing the 50 fine French cooking tips from the list box. You can remove items from a list box at design time simply by deleting them in the List property. At runtime, you use the RemoveItem() method. Heres an example; in this case, we add four items, Items 0 through 3 to a list box:
Private Sub Form_Load()
List1.AddItem ("Item 0")
List1.AddItem ("Item 1")
List1.AddItem ("Item 2")
List1.AddItem ("Item 3")
End Sub
Item 0 has index 0 in the list box, Item 1 has index 1, and so on. To remove, say, Item 1 when the user clicks a command button, we can use RemoveItem and pass it the items index:
Private Sub Command1_Click()
List1.RemoveItem 1
End Sub
Running the program and clicking the button gives the result shown in Figure 8.3. Now were able to remove items from a list box.
Sorting A List BoxYoure very proud of your new programs list box, which lists all the classical music recordings available for the last 40 years. But the Testing Department isnt so happy. They ask, Cant you alphabetize that list? You can alphabetize the items in a list box by setting its Sorted property to True (its False by default) at design time or runtime. Thats all it takes. (In fact, Ive known lazy programmers who sorted arrays of text by placing the text into a hidden list box and then read it back to save writing the code for the string comparisons!)
Determining How Many Items Are In A List BoxYou want to loop over the items in your list box to find out if a particular item is in the list, but you need to know how many items are in the list box in order to set up the loop. How can you set up the loop? You can use the ListCount property to determine how many items are in a list box. When setting up loops over the items in a list box, you should note that ListCount is the total number of items in a list, whereas index values start at 0, not 1. This means that if youre looping over indices, you should loop to ListCount 1, not ListCount. Lets see an example. Here, well search a list box to see if it has an item whose caption is Item 1. First, we set up the loop over the indexes of the items in the list box:
Private Sub Command1_Click()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To List1.ListCount - 1
...
Next intLoopIndex
End Sub
Then we check the caption of each item, checking for the caption Item 1, and report if we find that item:
Private Sub Command1_Click()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To List1.ListCount - 1
If List1.List(intLoopIndex) = "Item 1" Then
MsgBox "Found item 1!"
End If
Next intLoopIndex
End Sub
Determining If A List Box Item Is SelectedThe big point of list boxes is to let the user make selections, of course, and there are a number of properties to handle that process. Heres an overview. You get the index of the selected item in a list box with the ListIndex property. If no item is selected, ListIndex will be 1. You can get the text of a lists selected item as List1.Text or List1.List(List1.ListIndex). You can use a list boxs Selected array to determine if individual items in the list box are selected or not. Lets see an example to see how that works; in this case, well loop over the elements in the list box until we find the selected one. We start by loading items into the list box when the form loads:
Private Sub Form_Load ()
List1.AddItem ("Item 0")
List1.AddItem ("Item 1")
List1.AddItem ("Item 2")
List1.AddItem ("Item 3")
List1.AddItem ("Item 4")
List1.AddItem ("Item 5")
List1.AddItem ("Item 6")
List1.AddItem ("Item 7")
End Sub
When the user clicks a command button, we can indicate which item is selected in the list box by displaying that items caption in a message box. We just loop over all the items in the list box:
Private Sub Command1_Click ()
Dim intLoopIndex
For intLoopIndex = 0 To List1.ListCount - 1
...
Next intLoopIndex
End Sub
And we check the Selected array for each item to find the selected item:
Private Sub Command1_Click ()
Dim intLoopIndex
For intLoopIndex = 0 To List1.ListCount - 1
If List1.Selected(intLoopIndex) Then
MsgBox "You selected " & List1.List(intLoopIndex)
End If
Next intLoopIndex
End Sub
Note that list boxes can support multiple selections if you set their MultiSelect property to True. See the next topic in this chapter to see how to handle selections in multiselect list boxes.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |