Results 1 to 3 of 3

Thread: [RESOLVED] ListView Find improvement needed

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Resolved [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.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: ListView Find improvement needed

    ListView1.SelectedItem.EnsureVisible works great thanks

    i searched for auto-complete and found what i needed here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width