?
Solved

Listview: Position selected item at top of list

Posted on 2003-11-12
6
Medium Priority
?
730 Views
Last Modified: 2013-12-25
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!
0
Comment
Question by:rbroome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:bingie
ID: 9736257
Can you post your code so far?
0
 

Author Comment

by:rbroome
ID: 9736305
Private Sub txtPartNoOther_KeyUp(KeyCode 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(txtPartNoOther) '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).EnsureVisible
    End If
   
End Sub
0
 
LVL 39

Accepted Solution

by:
appari earned 2000 total points
ID: 9736366
try this

Private Sub txtPartNoOther_KeyUp(KeyCode 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(txtPartNoOther) '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).EnsureVisible
       
        Dim itmFound As ListItem
       
       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).EnsureVisible
           End If
           
        Next i
        itmFound.EnsureVisible
       
       
     
       
    End If
   
End Sub


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rbroome
ID: 9736425
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).EnsureVisible
'           End If
'          
'        Next i
        lvwOthers.ListItems(lvwOthers.ListItems.Count - 1).EnsureVisible
        itmFound.EnsureVisible


Thanks, and here are the points!
0
 
LVL 39

Expert Comment

by:appari
ID: 9736464

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.
0
 

Author Comment

by:rbroome
ID: 9736536
Yep, thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question