Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Removing Items From A List Box

The Testing Department is calling again—how about letting the users customize your program? You ask, what do you mean? Well, they say, let’s 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. Here’s 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 item’s 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 we’re able to remove items from a list box.


Figure 8.3  Removing an item from a list box.


TIP:  You should note that removing an item from a list box changes the indexes of the remaining items. After you remove Item 1 in the preceding example, Item 2 now gets index 1 and Item 3 gets index 2. If you want to change those indexes back to their original values, set the items’ Index properties.

Sorting A List Box

You’re very proud of your new program’s list box, which lists all the classical music recordings available for the last 40 years. But the Testing Department isn’t so happy. They ask, Can’t you alphabetize that list?

You can alphabetize the items in a list box by setting its Sorted property to True (it’s False by default) at design time or runtime. That’s all it takes. (In fact, I’ve 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!)


TIP:  You should know, however, that sorting a list box can change the indexes of the items in that list box (unless they were already in alphabetical order). After the sorting is finished, the first item in the newly sorted list has index 0, the next index 1, and so on. If you want to change the indexes of the items back to their original values, you can set their Index properties.

Determining How Many Items Are In A List Box

You 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 you’re looping over indices, you should loop to ListCount – 1, not ListCount.

Let’s see an example. Here, we’ll 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 Selected

The 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. Here’s 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 list’s selected item as List1.Text or List1.List(List1.ListIndex).

You can use a list box’s Selected array to determine if individual items in the list box are selected or not. Let’s see an example to see how that works; in this case, we’ll 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 item’s 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.


Previous Table of Contents Next


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.