* VB-CODE (1)
Tip 141: Searching a List Box Control for a Partial Match
August 6, 1995
The List Box control in Microsoft® Visual Basic® lets you display a
list of items
to the user. This article explains how you can search a List Box
control for a
specific entry by specifying a partial search string.
Finding Items in a List Box Control
The List Box control can be used within a Microsoft® Visual Basic®
maintain a list of items. While your program is running, you can use
property of the List Box control to determine how many items are stored
list. Then, using the ListCount value, you can search through the
contents of a
List Box control to find a specific item.
It's easy to write a procedure in Visual Basic to selectively find
items in a
List Box control. For example, if you want to search the List Box
control for the
item "oranges," you can use a For-Next loop to check each entry in the
control to see whether it matches the target string. To do this, use
For X = 0 To Lst.ListCount -1
If Lst.List(X) = "oranges" Then
'we found an item that matches.
The code routine above tells us whether the item "oranges" was found in
Box control, but what happens if we want to find a partial item in the
Let's suppose that each item in the list contains a phrase such as
oranges." You want to find the item that contains the word "oranges."
routine will only return a match if the entire string matches the word
To work around this problem, we can use the Visual Basic InStr function
each entry in the List Box control. The InStr function will return the
within the larger string where the target string is found. To use this
technique, you still need to include a For-Next loop to examine each
entry in the
List Box control; however, you can also add code to call the InStr
determine whether a specific portion of an entry matches your target
Each time the InStr function finds the target string in an entry in the
control, it returns the target string's position within the entry. Just
ListIndex property of the List Box control to retrieve the entry that
your target string.
This program shows how to search a List Box control for a partially
1. Create a new project in Visual Basic. Form1 is created by default.
2. Add the following code to the Form_Load event for Form1.
Private Sub Form_Load()
List1.AddItem "Apples and oranges"
List1.AddItem "Bananas and grapes"
List1.AddItem "Peaches and corn"
3. Add a List Box control to Form1. List1 is created by default.
4. Add a Text Box control to Form1. Text1 is created by default.
5. Add a Command Button control to Form1. Command1 is created by
6. Add the following code to the Click event for Command1.
Private Sub Command1_Click()
7. Create a new procedure called GetPartialString. Add the following
Dim LittleString As String
Dim Item As Integer
LittleString = Text1.Text
Item = GetMatch(List1, LittleString)
If Item = -1 Then
MsgBox "No such entry found in List Box"
List1.ListIndex = Item%
8. Create a new procedure called GetMatch. Add the following code to
Function GetMatch(Lst As ListBox, ByVal SearchStr As String) As Integer
Dim X As Integer
For X = 0 To Lst.ListCount - 1
If InStr(Lst.List(X), SearchStr) Then
GetMatch = X
GetMatch = -1 'no match
Run the demonstration program by pressing F5. Three items are displayed
List Box control. Type a word such as "corn" in the Text Box control
the command button. The program highlights the "Peaches and corn" entry
List Box control because the word "corn" was found in this entry. Type
"turnip" in the Text Box control. After you click the command button, a
box is displayed that tells you no such item was found.
Knowledge Base Q119738. "How to Quickly Search a List Box."
"List Box Controls." (Development Library, Technical Articles, Windows
User Interface Articles, Controls)