Results 1 to 6 of 6

Thread: Auto Completing ComboBox without API

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    248

    Auto Completing ComboBox without API

    I made this up with a lot of testing and playing. I didn't want to use API (I am always scared if I use to much API my app won't work on another win version).

    AddressBar is my ComboBox.


    VB Code:
    1. Private Sub AddressBar_KeyUp(KeyCode As Integer, Shift As Integer)
    2.  
    3. ' Set UserTyped as what the user themselves have typed
    4. UserTyped = AddressBar.Text
    5.  
    6. ' Action is based on current key press
    7. Select Case KeyCode
    8.  
    9.   ' If the keypress is Left, Right,
    10.   ' Up, Down, PgUp, PgDown
    11.   ' Back and Ctrl, then just exit sub
    12.   Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyShift, vbKeyEscape, vbkeydel, vbKeyPageUp, vbKeyPageDown, vbKeyBack, vbKeyControl
    13.     Exit Sub
    14.    
    15.   ' Another key was pressed so lets try to fill in
    16.   ' Addressbar if there is a simular address
    17.   Case Else
    18.     ' TopIndex holds the index that is currently at the top
    19.     ' of the combobox (Favorites). Even thought the combobox
    20.     ' does not expand it is automatically scrolling to whatever
    21.     ' is the closest text in it's list to what is typed
    22.     ' in the box. So TopIndex will hold either the first
    23.     ' item in the list or whatever it thinks is the closest
    24.     ' to what is being typed
    25.     '
    26.     ' If TopIndex is greater than 0 the combobox (Favorites)
    27.     ' has found simular text in it's list
    28.     ' OR
    29.     ' If TopIndex is still 0 we want to see if it is becuase the user
    30.     ' is typing something simular to what is in index 0
    31.     If ((AddressBar.TopIndex > 0) Or (InStr(1, AddressBar.List(AddressBar.TopIndex), UserTyped))) Then
    32.       ' Make the Addressbar.Text hold what the user has typed
    33.       ' and what the computer things is like it
    34.       AddressBar.Text = UserTyped & Mid(AddressBar.List(AddressBar.TopIndex), Len(UserTyped) + 1, Len(AddressBar.List(AddressBar.TopIndex)))
    35.       ' Set the starting point of selection to after what
    36.       ' the user typed
    37.       AddressBar.SelStart = Len(UserTyped)
    38.       ' The length of selection is to the end
    39.       AddressBar.SelLength = Len(AddressBar.Text) - Len(UserTyped)
    40.     End If
    41.  
    42. End Select
    43.  
    44.  
    45. End Sub

    It checks the ComboBox to see if any item in it compares to what is being typed.
    If so it then adds it to the end of what is being typed and selects the text that is being added so the user can continue to type over it.

    Of course there are tons of things you can do the add to this but it is a working example.

    Let me know if this helps anyone
    Mike
    Last edited by MikeJoel; Feb 13th, 2006 at 11:15 AM.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [SHARING] Auto Completing ComboBox without API

    If you want to share code snippets, please use the code bank

  3. #3
    Addicted Member Quizton's Avatar
    Join Date
    Dec 2005
    Location
    VB Forums
    Posts
    209

    Re: [SHARING] Auto Completing ComboBox without API

    Great code Mike
    can you also do this with listing a recorset out of a table in a db?

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [SHARING] Auto Completing ComboBox without API

    Moved to the CodeBank

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    248

    Re: Auto Completing ComboBox without API

    Sorry (I didn't know there was a codebank).


    I am not sure how it would work with a db. It would of course have to be adjusted. It basically uses the fact that the combo box automatically scrolls to the closest item matching what is entered (you just don't see it happening). My code just checks to see if it has scrolled and then copies the indexed text (past the point the user typed) to the end.

    So how that would work with a db I don't know.

  6. #6
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: Auto Completing ComboBox without API

    i would say, just read from the db and put it into the combobox using the addnext in the form load or some other trigger that will change the contents of the autocomplete.

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