Results 1 to 6 of 6

Thread: Auto Completing ComboBox without API

Threaded View

  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.

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