rbroome
asked on
Listview: Position selected item at top of list
I have a textbox and a listview. When you start typing in the textbox, it searches through the listview and highlights the first item that matches.
I use .ensurevisible to make sure the item is on the screen.
This all works fine, except that the highlighted item appears at the bottom of the listview. I'd like it to automatically scroll the listview so that the matching item is the first item visible in the listview.
Any idea how to achieve this? I have tried lots of things, but nothing seems to work, so I"m happy to give maximum points to whoever can give me a solution!
I use .ensurevisible to make sure the item is on the screen.
This all works fine, except that the highlighted item appears at the bottom of the listview. I'd like it to automatically scroll the listview so that the matching item is the first item visible in the listview.
Any idea how to achieve this? I have tried lots of things, but nothing seems to work, so I"m happy to give maximum points to whoever can give me a solution!
Can you post your code so far?
ASKER
Private Sub txtPartNoOther_KeyUp(KeyCo de As Integer, Shift As Integer)
Dim X As Long
If Len(txtPartNoOther) <= 1 Then
lvwOthers.ListItems.Clear
Exit Sub
Else
'Don't fill the list until at least 2 characters have been entered
If Len(txtPartNoOther) = 2 _
And KeyCode <> 8 Then 'backspace
If DBOK Then
Call LoadXRefData(txtPartNoOthe r) 'this fills the listview
Else
txtPartNoOther = ""
Exit Sub
End If
End If
End If
X = InListView(Me.lvwOthers, txtPartNoOther.Text)
'this function does a binary search of the listview and returns the index of the matching item
If X > -1 Then
Set lvwOthers.SelectedItem = lvwOthers.ListItems(X)
lvwOthers.ListItems(X).Ens ureVisible
End If
End Sub
Dim X As Long
If Len(txtPartNoOther) <= 1 Then
lvwOthers.ListItems.Clear
Exit Sub
Else
'Don't fill the list until at least 2 characters have been entered
If Len(txtPartNoOther) = 2 _
And KeyCode <> 8 Then 'backspace
If DBOK Then
Call LoadXRefData(txtPartNoOthe
Else
txtPartNoOther = ""
Exit Sub
End If
End If
End If
X = InListView(Me.lvwOthers, txtPartNoOther.Text)
'this function does a binary search of the listview and returns the index of the matching item
If X > -1 Then
Set lvwOthers.SelectedItem = lvwOthers.ListItems(X)
lvwOthers.ListItems(X).Ens
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
appari,
Thanks for that. It worked, but was extremely slow (with over 1000 items in the Listview). But you pointed me in the right direction - I tweaked your code to the following:
Set itmFound = lvwOthers.ListItems(X)
itmFound.EnsureVisible
itmFound.Selected = True
Dim i As Integer
Dim blnSelected As Boolean
blnSelected = False
' For i = 1 To lvwOthers.ListItems.Count
' If lvwOthers.ListItems.Item(i ).Selected Then blnSelected = True
' If blnSelected Then
' lvwOthers.ListItems.Item(i ).EnsureVi sible
' End If
'
' Next i
lvwOthers.ListItems(lvwOth ers.ListIt ems.Count - 1).EnsureVisible
itmFound.EnsureVisible
Thanks, and here are the points!
Thanks for that. It worked, but was extremely slow (with over 1000 items in the Listview). But you pointed me in the right direction - I tweaked your code to the following:
Set itmFound = lvwOthers.ListItems(X)
itmFound.EnsureVisible
itmFound.Selected = True
Dim i As Integer
Dim blnSelected As Boolean
blnSelected = False
' For i = 1 To lvwOthers.ListItems.Count
' If lvwOthers.ListItems.Item(i
' If blnSelected Then
' lvwOthers.ListItems.Item(i
' End If
'
' Next i
lvwOthers.ListItems(lvwOth
itmFound.EnsureVisible
Thanks, and here are the points!
yeah i tested it on small set of data so it was quick:), after posting i thought of the alternative, same as yours, first make last item visible then the actual item.
ASKER
Yep, thanks!