-
I have a listbox sorted alphabetically, with a text box above. What I want to do is allow the user to type in a search in the text box and as they type the list box selection changes gradually.
E.g if my list is
Arsenal
Chelsea
Man City
Man United
as the user types 'M' it moves to Man City and then stays there unless they type 'an u' which then changes the list to man united.
any ideas?
-
Are you really sure you want to do it that way? The search box will contain "Mu" not "Man U" i could make you a fast binary searh if you want.
-
my list has about 350 items and i want the user to type in a search and the list moves to the nearest item
-
Here goes, this binary search should find you as fast as possible, remove the lcases if you want case sensistive search.
Code:
Private Sub Text1_Change()
List1.ListIndex = BinarySearch(Text1.Text, List1)
End Sub
Function BinarySearch(Byval str As String, list As ListBox)
Dim x&, l&, temp$
str = lcase(str)
l = List1.ListCount / 2
x = l
Do
temp = lcase(list.list(x))
If str = temp Then
Exit Do
ElseIf str > temp Then
l = l / 2
x = x + l
Else
l = l / 2
x = x - l
End If
Loop While l
BinarySearch = x
End Function