|
-
Feb 12th, 2006, 04:59 PM
#1
Thread Starter
Addicted Member
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:
Private Sub AddressBar_KeyUp(KeyCode As Integer, Shift As Integer)
' Set UserTyped as what the user themselves have typed
UserTyped = AddressBar.Text
' Action is based on current key press
Select Case KeyCode
' If the keypress is Left, Right,
' Up, Down, PgUp, PgDown
' Back and Ctrl, then just exit sub
Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyShift, vbKeyEscape, vbkeydel, vbKeyPageUp, vbKeyPageDown, vbKeyBack, vbKeyControl
Exit Sub
' Another key was pressed so lets try to fill in
' Addressbar if there is a simular address
Case Else
' TopIndex holds the index that is currently at the top
' of the combobox (Favorites). Even thought the combobox
' does not expand it is automatically scrolling to whatever
' is the closest text in it's list to what is typed
' in the box. So TopIndex will hold either the first
' item in the list or whatever it thinks is the closest
' to what is being typed
'
' If TopIndex is greater than 0 the combobox (Favorites)
' has found simular text in it's list
' OR
' If TopIndex is still 0 we want to see if it is becuase the user
' is typing something simular to what is in index 0
If ((AddressBar.TopIndex > 0) Or (InStr(1, AddressBar.List(AddressBar.TopIndex), UserTyped))) Then
' Make the Addressbar.Text hold what the user has typed
' and what the computer things is like it
AddressBar.Text = UserTyped & Mid(AddressBar.List(AddressBar.TopIndex), Len(UserTyped) + 1, Len(AddressBar.List(AddressBar.TopIndex)))
' Set the starting point of selection to after what
' the user typed
AddressBar.SelStart = Len(UserTyped)
' The length of selection is to the end
AddressBar.SelLength = Len(AddressBar.Text) - Len(UserTyped)
End If
End Select
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.
-
Feb 12th, 2006, 07:34 PM
#2
Re: [SHARING] Auto Completing ComboBox without API
If you want to share code snippets, please use the code bank
-
Feb 12th, 2006, 09:44 PM
#3
Addicted Member
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?
-
Feb 13th, 2006, 07:35 AM
#4
Re: [SHARING] Auto Completing ComboBox without API
-
Feb 13th, 2006, 11:17 AM
#5
Thread Starter
Addicted Member
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.
-
Mar 5th, 2006, 04:39 AM
#6
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|