Control - Listbox  
Adding list item at design-time
Adding list item at run-time
Clearing a list
Counting list entries
Getting selected items index
Removing items
Searching the list items
Storing numbers to the ItemData property
     Adding list item at design-time  
To add items to a list at design-time, select the 'List' property in the properties window. Enter an item and then hit Control+Enter to enter the next one. Once you have entered them all hit the Enter key.
     Adding list item at run-time  
To add an item simply use the AddItem method:

 List1.AddItem myitem, index

'myitem' would be the item you want to add. And 'index' is an optional parameter that allows you to specify where on the list you wish wo add the new item. If you omit the 'index' parameter then the item will just be added to the end of the list.

     Clearing a list  
To clear a list, use the Clear method:

List1.Clear

     Counting list entries  
To count how many entries are in a listbox use the ListCount method:

Dim entries As Integer
entries = List1.ListCount

 

     Getting Selected items index  
For a single item its relatively easy. Just check to see if theres a ListIndex and if so then thats the selected item.

'for instance to remove a selected item
If List1.ListIndex >= 0 Then
     List1.RemoveItem List1.ListIndex
End If

To see how many items are selected use the SelCount property. That will hold an integer value of how many items are selected. If its more then one then you have multiple selections.

Now for multiselections you have to run a loop and use the 'Selected' property. The 'Selected' property of the listbox is actually an array that represents each item in the listbox, but if an item is selected then the Selected property of that listitem will be set to True. For example :

If List1.SelCount > 1 Then
    For i = List1.ListCount - To 0 Step -1
        If List1.Selected(i) Then
           
'this item is selected
        End If
    Next i
End If

 

     Removing items  
Just use the RemoveItem method. You just have to know the index of the item you want to remove.

List1.RemoveItem index

if you want to just remove the first item in the list, use a 0

List1.RemoveItem 0

to remove a selected item

If List1.ListIndex >= 0 Then
     List1.RemoveItem List1.ListIndex
End If

 

     Searching the list items  
If you are looking for an exact match, it can be quite simple. All you have to do is set the ListBoxes Text property to the word you want to find.  If it returns -1 then it was not found.

Function SearchList( Text As String ) As Integer
    lstNames.Text = Text 
    SearchList = lstNames.ListIndex
End Function

If you want to do a partial word search, such as for Auto-Completion, then its going to take a little more work. You'll need to set your list's 'Sorted' property to True. Then when you do the search, you'll take the middle list item, check to see if its lower or higher than your word, and basically keep doing that, splitting the list everytime. 

    Storing numbers to the ItemData property  
Yes Listboxes store data other then what you actually see. This can be a very handy feature. The only thing is, it can only store Long Integer data, not strings. But it can still be quite useful...

What it is is an array that coincides with the ListIndex of your ListItems. So to access it for saving or retrieving data, just enter the list index for the item you want in the ItemData element.

List1.AddItem "NewName"
List1.ItemData(List1.NewIndex) = 37921