Jag hittade denna funktion i msdn som hittar alla lika strängar i en listbox. Problemet är att sista värdet i listboxen inte får vara den sträng man söker efter. Det måste vara något annat värde. Någon som kan förklara för mig varför det blir så? Hitta alla strängar i listbox
Felet blir alltså: "Specified argument was out of the range of valid values" på denna rad:
x = ListBox1.FindStringExact(searchString, x)
************************************************************************************
Private Sub FindAllOfMyExactStrings(ByVal searchString As String)
' Set the SelectionMode property of the ListBox to select multiple items.
ListBox1.SelectionMode = SelectionMode.MultiExtended
' Set our intial index variable to -1.
Dim x As Integer = -1
' If the search string is empty exit.
If searchString.Length <> 0 Then
' Loop through and find each item that matches the search string.
Do
' Retrieve the item based on the previous index found. Starts with -1 which searches start.
x = ListBox1.FindStringExact(searchString, x)
' If no item is found that matches exit.
If x <> -1 Then
' Since the FindStringExact loops infinitely, determine if we found first item again and exit.
If ListBox1.SelectedIndices.Count > 0 Then
If x = ListBox1.SelectedIndices(0) Then
Return
End If
End If
' Select the item in the ListBox once it is found.
ListBox1.SetSelected(x, True)
End If
Loop While x <> -1
End If
End Sub
************************************************************************************