Two of the most useful methods of the ListBox control are the FindString and FindStringExact methods, which allow you to quickly locate any item in the list. The FindString method locates a string that partially matches the one you’re searching for; FindStringExact finds an exact match. If you’re searching for Man, and the control contains a name such as Mansfield, FindString matches the item, but FindStringExact does not.
Both the FindString and FindStringExact methods perform case-insensitive searches. If you’re searching for visual, and the list contains the item Visual, both methods will locate it. Their syntax is the same:
itemIndex = ListBox1.FindString(searchStr As String)Code language: VB.NET (vbnet)
where searchStr is the string you’re searching for. An alternative form of both methods allows you to specify the order of the item at which the search will begin:
itemIndex = ListBox1.FindString(searchStr As String, startIndex As Integer)Code language: VB.NET (vbnet)
The startIndex argument allows you to specify the beginning of the search, but you can’t specify where the search will end.
The FindString and FindStringExact methods work even if the ListBox control is not sorted. You need not set the Sorted property to True before you call one of the searching methods on the control. Sorting the list will help the search operation, but it takes the control less than 100 milliseconds to find an item in a list of 100,000 items, so time spent to sort the list isn’t worth it. Before you load thousands of items in a ListBox control, however, you should probably consider a more-functional interface.
The ListBoxSearch Application
The application you’ll build in this section (seen in Figure 4.5) populates a list with a large number of items and then locates any string you specify. Click the button Populate List to populate the ListBox control with 10,000 random strings. This process will take a few seconds and will populate the control with different random strings every time. Then, you can enter a string in the TextBox control at the bottom of the form. As you type characters (or even delete characters in the TextBox), the program will locate the closest match in the list and select (highlight) this item.
Figure 4.5 – ListBox Control Search example
The sample application reacts to each keystroke in the TextBox control and locates the string you’re searching for instantly. The Find Item button does the same, but I thought I should demonstrate the efficiency of the ListBox control and the type of functionality you’d expect in a rich client application.
The code (shown in Listing 4.13) attempts to locate an exact match via the FindStringExact method. If it succeeds, it reports the index of the matching element. If not, it attempts to locate a near match with the FindString method. If it succeeds, it reports the index of the near match (which is the first item on the control that partially matches the search argument) and terminates. If it fails to find an exact match, it reports that the string wasn’t found in the list.
Listing 4.13: Searching the List
Private Sub TextBox1_TextChanged(...) Handles TextBox1.TextChanged Dim srchWord As String = TextBox1.Text.Trim If srchWord.Length = 0 Then Exit Sub Dim wordIndex As Integer wordIndex = ListBox1.FindStringExact(srchWord) If wordIndex >= 0 Then ListBox1.TopIndex = wordIndex ListBox1.SelectedIndex = wordIndex Debug.WriteLine("EXACT MATCH AT INDEX = " & wordIndex.ToString & vbCrLf & _ "MATCH = " & ListBox1.Items(wordIndex).ToString) Else wordIndex = ListBox1.FindString(srchWord) If wordIndex >= 0 Then ListBox1.TopIndex = wordIndex ListBox1.SelectedIndex = wordIndex Debug.WriteLine("NEAR MATCH AT INDEX = " & wordIndex.ToString & vbCrLf & _ "MATCH = " & ListBox1.Items(wordIndex).ToString) Else Debug.WriteLine("Item " & srchWord & " is not in the list") End If End If End SubCode language: VB.NET (vbnet)
If you search for SAC, for example, and the control contains a string such as “SAC” or “sac” or “sAc”, the program will return the index of the item in the list and will report an exact match. If no exact match can be found, the program will return something like “SACDEF”, if such a string exists on the control, as a near match. If none of the strings on the control starts with the characters SAC, the search will fail.
Populating the List
The Populate List button creates 10,000 random items with the help of the Random class. First, it generates a random value in the range 1 through 20, which is the length of the string (not all strings have the same length). Then the program generates as many random characters as the length of the string and builds the string by appending each character to it. These random numbers are in the range of 65 to 91 and they’re the ANSI values of the uppercase characters.