[RESOLVED] ListView Find improvement needed
Im using the following code in a command button to type in a text box a the text i want to seach for in a certain column in my apps listview.
Code:
Dim X As Integer
X = Len(Text2.Text)
For i = 1 To ListView1.ListItems.Count
If UCase(Left$(ListView1.ListItems.Item(i).SubItems(1), X)) = UCase(Text2.Text) Then
ListView1.ListItems.Item(i).Selected = True
ListView1.SetFocus
Exit For
End If
Next
this works fine but i would like to have a way where the listview has focus and while i type is starts to find and move to the record with focus to that row. the above code also doesn't move to vertical scroll bar down, or up to the selected row.
Re: ListView Find improvement needed
Search the forum for "auto-complete". Most of those examples use the listbox or combobox as the target, but the logic may be applicable.
1. To ensure the selected item is seen: ListView1.SelectedItem.EnsureVisible
2. Searching the entire listview via a loop can be time consuming if you have a lot of entries. If not used, you may want to store the value of .SubItem(1) in the .Tag property of the listitem. This would allow you to use the ListView's .FindItem method:
Code:
Dim tItem As ListItem
Set tItem = ListView1.FindItem(Text2.Text, lvwTag)
If Not tItem Is Nothing Then
Set ListView1.SelectedItem = tItem
tItem.EnsureVisible
Else ' message not found?
End If
The .FindItem method has more parameters that can be useful, like which Index to start searching from. Read your help files.
Re: ListView Find improvement needed
ListView1.SelectedItem.EnsureVisible works great thanks
i searched for auto-complete and found what i needed here