Results 1 to 13 of 13

Thread: Auto-Complete partially done

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Auto-Complete partially done

    I have a text box and a '4' column ListView on a form.
    When the user types into the text box I have got it to auto-complete and also highlight the selection in the listview.

    What I would like it to also do is:
    • Check against a different column in the listview than the first column
    • Scroll the listview to show the selected item
    • Highlight the listview as in THIS threads post #1


    I have used this code, from the above thread, which partially does what I am after:

    vb Code:
    1. Private Sub txtSearch_Change()
    2.  
    3. 'Run through the available items and grab the first matching one.
    4. Dim LvwItm As ListItem
    5. Dim Strt As Integer
    6.  
    7. lvwMembers.FindItem(txtSearch.Text, , , 1).Selected = True
    8.  
    9. 'If something was chosen then we get the rest of it
    10. Strt = Len(txtSearch.Text)
    11. txtSearch.Text = lvwMembers.SelectedItem.Text
    12. txtSearch.SelStart = Strt
    13. txtSearch.SelLength = Len(txtSearch.Text) - Strt
    14.  
    15. End Sub
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Auto-Complete partially done

    I found this in a post by RobDog888,

    Quote Originally Posted by RobDog888
    These are the parameters for the finditem method -
    .FindItem (string, value, index, match)

    string: string to be search for.
    value: lvwText, lvwSubitem, lvwTag
    index: specifies the index location from which to begin the search.
    match: lvwWholeWord, lvwPartial
    So I have tried to use this to get mine to search on the third column of the listview, but I get the error:

    Object variable or With block variable not set

    on this line of code:

    vb Code:
    1. lvwMembers.FindItem(txtSearch.Text, lvwSubItem, 2, lvwPartial).Selected = True
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Auto-Complete partially done

    Sounds like it isn't finding a match. Maybe declare a variable As ListItem, Set that variable to the result of FindItem, and check if the variable Is Nothing before trying to set it as the SelectedItem?

    Also, my interpretation of the Index parameter is that it isn't talking about which SubItem, but rather which Item in the list. So I read your call as searching within all SubItems, starting with the 2nd ListItem.

    I could be wrong, though.

  4. #4

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Auto-Complete partially done

    Quote Originally Posted by Ellis Dee
    Sounds like it isn't finding a match. Maybe declare a variable As ListItem, Set that variable to the result of FindItem, and check if the variable Is Nothing before trying to set it as the SelectedItem?
    Your correct, it is returning Nothing. I used the following to check:
    vb Code:
    1. If TypeName(lvwMembers.FindItem(txtSearch.Text, lvwSubItem, , lvwPartial)) <> "Nothing" Then
    2.     MsgBox "something"
    3. Else
    4.     MsgBox "Nothing"
    5. End If

    It should be finding something though! There is data to find!

    Quote Originally Posted by Ellis Dee
    Also, my interpretation of the Index parameter is that it isn't talking about which SubItem, but rather which Item in the list. So I read your call as searching within all SubItems, starting with the 2nd ListItem.

    I could be wrong, though.
    If you are wrong, then how would I specify the third column?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Auto-Complete partially done

    Just an FYI: When checking whether an object is valid or not, you don't need to use the TypeName function. This is simpler:
    vb Code:
    1. If lvwMembers.FindItem(txtSearch.Text, lvwSubItem, , lvwPartial) Is Nothing Then
    I wish I could help you more, but I honestly haven't a clue how to specify which subitem to search. I tend to doubt it's even possible without manually writing a the search yourself. (eg: Iterating through all the list items, comparing each list item's appropriate SubItem/column against your criteria. Sloooooooowwwww.)

  6. #6

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Auto-Complete partially done

    How about when I load the listview, I load a hidden listview with the items from the third column and the row number. Match the row number with the first listview and select it that way?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  7. #7

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Auto-Complete partially done

    Anybody else help with the three following 'challenges'?

    • Check against a different column in the listview than the first column
    • Scroll the listview to show the selected item
    • Highlight the listview as in THIS threads post #1
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  8. #8

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Auto-Complete partially done

    *Anybody*
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  9. #9
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Auto-Complete partially done

    No, don't bother with hidden listviews; that would be a tremendous waste of resources even if you put them into a control array.

    Better would be to create an array and add all the listitems and subitems to it for easy searching.

    The more-readable but less portable method would be to use a UDT array. Something like:
    Code:
    Private Type ListDataType
        Index As Long
        Column(0 To 2) As String
    End Type
    
    Private udtList() As ListDataType
    
    Sub LoadList()
    
    Do
        Add all items to ListView
    Loop
    With ListView
        ReDim Preserve udtList(.ListItems.Count)
        For i = 0 To .ListItems.Count
            With .ListItem(i)
                udtList(i).Index = i
                udtList(i).Columns(0) = .Text
                udtList(i).Columns(1) = .SubItem(1)
                udtList(i).Columns(2) = .SubItem(2)
            End With
        Next
    End With
    Obviously this is poorly thrown-together pseudocode.

    The downside to this approach is that sorting udt arrays is annoying; you have to write non-reusable code specific to that UDT. If the list is reasonably small you can ignore sorting & searching altogether and just use a brute-force iteration to find matches.

  10. #10

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Auto-Complete partially done

    Thanks again Ellis

    I have decided to leave it that the search is on the first column only. It's not a major problem, I just thought it would have been a nice touch that was all.

    (That said, if anybody does find a way to search one of the SubItems - please feel free to post )
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Auto-Complete partially done

    You'll have to loop cause lvwPartial won't work with lvwSubitem.

    http://www.vbforums.com/showthread.p...ght=lvwpartial

    I'm not aware of any API solution for the listview find.

  12. #12
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Auto-Complete partially done

    Hi aikidokid, that is not a listview in the other thread. It's a listbox.

    Here is a sample autocomplete listview.

    Code:
    Dim DelKey As Boolean
    Private Sub Text1_Change()
    On Error GoTo ErrMsg
    
    Dim LstItm As ListItem
        ListView1.FindItem(Text1.Text, , , 1).Selected = True
           
        If Not DelKey Then
            'If something was chosen then we get the rest of it
            strt = Len(Text1.Text)
            Text1.Text = ListView1.SelectedItem.Text
            Text1.SelStart = strt
            Text1.SelLength = Len(Text1.Text) - strt
        End If
        
        DelKey = False
       
    Exit Sub
    ErrMsg:
        ListView1.SelectedItem.Selected = False
    End Sub
    
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyDelete Or KeyCode = 8 Then DelKey = True
    End Sub
    
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        'Add some items to the listbox
        With ListView1.ListItems
            .Add , , "Computer"
            .Add , , "Screen"
            .Add , , "Modem"
            .Add , , "Printer"
            .Add , , "Scanner"
            .Add , , "Sound Blaster"
            .Add , , "Keyboard"
            .Add , , "CD-Rom"
            .Add , , "Mouse"
        End With
    End Sub
    Just play with it.

  13. #13
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Auto-Complete partially done

    Here is autocomplete for listbox.

    Code:
    Option Explicit
    'API call to listbox
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
        ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        ByRef lParam As Any _
    ) As Long
    Const LB_FINDSTRING = &H18F
    Dim DelKey As Boolean
    Dim bNoClick As Boolean
    
    Private Sub List1_Click()
    'to prevent executing the click event
     If bNoClick Then Exit Sub
        Text1.Text = List1.Text
    End Sub
    
    Private Sub List1_GotFocus()
        SendKeys "{DOWN}"
    End Sub
    
    Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
        List1_Click
    End Sub
    
    Private Sub Text1_Change()
    'autocomplete feature
    Dim strt As Long, nIndex As Long
    Dim nLen As Long, sText As String
    Const LB_GETTEXTLEN As Long = &H18A
    Const LB_GETTEXT As Long = &H189
    Static blnBusy As Boolean
    
    If blnBusy Then
       Exit Sub
    End If
         
         bNoClick = True
         blnBusy = True
        
        'Retrieve the item's listindex
        List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
        
        If Not DelKey Then
    
    
        If List1.ListIndex <> -1 Then
            strt = Len(Text1.Text)
            Text1.Text = List1.List(List1.ListIndex)
            Text1.SelStart = strt
            Text1.SelLength = Len(Text1.Text) - strt
        Else
        
        End If
        End If
           DelKey = False
           blnBusy = False
           bNoClick = False
    
    End Sub
    
    Private Sub Text1_Click()
        'select all the text
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1)
    End Sub
    
    Private Sub text1_KeyDown(KeyCode As Integer, Shift As Integer)
        'for delete and backspace
        If KeyCode = vbKeyDelete Or KeyCode = 8 Then
        
        DelKey = True
        Exit Sub
    
        ElseIf KeyCode = vbKeyDown Then
            List1.SetFocus
        End If
    End Sub
    That is assuming, the listbox has already items on it. Try it out and see what fits your program best.

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