-
Apr 29th, 2009, 07:52 AM
#1
Thread Starter
Addicted Member
combobox select as you type
i want make a combobox work in such a way that as i am typing the first characters of a word in the list the hi-light shifts to the item whose first characters matches those typed.i am stuck.
-
Apr 29th, 2009, 07:58 AM
#2
Re: combobox select as you type
use sendmessage API with cbfindsting
search on cbfindsting
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 29th, 2009, 01:06 PM
#3
Re: combobox select as you type
Here is a NON-API way
Code:
Option Explicit
Private blnBackSpace As Boolean
Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
If Combo1.Text <> vbNullString Then
'Let the Change event know that it shouldn't respond to this change.
blnBackSpace = True
End If
End If
End Sub
Private Sub Combo1_Change()
Dim i As Long
Dim lngText As Long
'If firing in response to a backspace or delete, don't run the auto-complete
'complete code. (Otherwise you wouldn't be able to back up.)
If blnBackSpace = True Or Combo1.Text = vbNullString Then
blnBackSpace = False
Exit Sub
End If
'Run through the available items and grab the first matching one.
For i = 0 To Combo1.ListCount - 1
If InStr(1, Combo1.List(i), Combo1.Text, vbTextCompare) = 1 Then
'Save the SelStart property.
lngText = Combo1.SelStart
Combo1.Text = Combo1.List(i)
'Set the selection in the combo.
Combo1.SelStart = lngText
Combo1.SelLength = Len(Combo1.Text) - lngText
Exit For
End If
Next
End Sub
-
Apr 30th, 2009, 01:51 AM
#4
Thread Starter
Addicted Member
Re: combobox select as you type
i tried the non api code but i dont see any response. the combo is not responding at all by showing a hi-light/cursor moving to the matching item in the list. is there anything else that i need to do. i have created a test form and i have added a combobox with its default name,combo1 and then populated it with items.then i just plugged in your code but i dont see any response. as for the first api suggestion...i dont understand at all. i am still a novice and you can help if you gave me the code. either solution is welcome...what ever does the job for me.
-
Apr 30th, 2009, 02:05 AM
#5
Lively Member
Re: combobox select as you type
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const CB_FINDSTRING = &H14C 'for comboboxes
-------------------------------------------------------------------------------
Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim Idx As Long, TempString As String
If KeyCode < 32 Or KeyCode > 126 Then Exit Sub
If KeyCode = vbKeyBack Then Exit Sub
TempString = Combo1.Text
Idx = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal TempString)
If Idx <> -1 Then
Combo1.ListIndex = Idx
Combo1.SelStart = Len(TempString)
Combo1.SelLength = Len(Combo1.Text) - Len(TempString)
Else
Combo1.Text = TempString
Combo1.SelStart = Len(TempString)
End If
End Sub
-
Apr 30th, 2009, 02:36 AM
#6
Thread Starter
Addicted Member
Re: combobox select as you type
yes...this seems to be what i am looking for..thanks a lot..but i realised it was working only when i am typing and if i hit the back space to search with the previous string then it stops working. i checked in the code and i commented out these lines
['If KeyCode < 32 Or KeyCode > 126 Then Exit Sub
'If KeyCode = vbKeyBack Then Exit Sub]
and now it seems to be working. was there a particular reason why you had included that condition...let me know lest i do something that might turn out catestrophic.
-
Apr 30th, 2009, 03:21 AM
#7
Thread Starter
Addicted Member
Re: combobox select as you type
ok...now my adjustments dont seem to be working now that i am using a bigger list from the database. so i uncommented it.once again i thank you.i am good now.
-
Apr 30th, 2009, 03:55 AM
#8
Thread Starter
Addicted Member
Re: combobox select as you type;Problem
Problem:
it appears when type in my characters and then press enter on the hi-lighted choice and with the text of the selected item appearing on the combo..it is not necessarily the item that the combo has selected..its index is in fact still pointing to the previously selected item. in such a way that if i have just loaded my form with the combobox not having a default value(when the index is -1), if i then do my search and press enter i get an "index out of range" error.if say i have picked one with the mouse...then follow up to pick another after my search by pressing enter..the value being returned is that of the previously selected item. however if i pick with the mouse it works just fine.but i dont want to use a mouse because my solution needs to work in retail enviroment where keys are much faster.
-
Apr 30th, 2009, 04:08 AM
#9
Lively Member
Re: combobox select as you type
hmmm.. that's odd.. the code is working perfectly to me...
-
Apr 30th, 2009, 04:34 AM
#10
Thread Starter
Addicted Member
Re: combobox select as you type
on the surface it appears as though you have selected the correct item but when you now use the value elsewhere thats when you notice you are working with the previous index.
-
Apr 30th, 2009, 04:50 AM
#11
Re: combobox select as you type
what do you want it to do when you press enter?
post the code you are using now
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 30th, 2009, 05:43 AM
#12
Thread Starter
Addicted Member
Re: combobox select as you type
the actual code that i want to use spans over several controls but i have done a small simulation of runing this simple code on a button,
<MsgBox "" & Me.combo1.ListIndex>,
FIRST when i type select for the item in the combo and press enter and there after clicking the button and SECOND when i pick the item with my mouse and there after clicking the button...i am getting two different values of listindex. the actual code will then need the listindex to access the itemdata of the selected item for other functions.
-
Apr 30th, 2009, 11:53 AM
#13
Thread Starter
Addicted Member
Re: combobox select as you type
guys...has anyone confirmed this unusual behavior or its just me..perhaps there is something else that i need to do
-
Apr 30th, 2009, 04:35 PM
#14
Re: combobox select as you type
as i can't see what you are doing i can not suggest how to fix
the code posted by wired changes the listindex any time a different item is selected by pressing a key, pressing the enter key does nothing , but the item is already selected
to do something by pressing the enter key you need to put code for that keycode, the enter key is currently bypassed by
If KeyCode < 32 Or KeyCode > 126 Then Exit Sub as it is keycode 13
this may not apply in your case, but most time searching the combo, in a long list, takes you close to the required item so you can select easily from the list
an example of this is you may have multiple items like earlyeuropeansettlementitem1 to 12
pressing e might get you to the first, but you would have to type the whole string to get any other
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 5th, 2017, 10:17 AM
#15
New Member
Re: combobox select as you type
Originally Posted by __wired__
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const CB_FINDSTRING = &H14C 'for comboboxes
-------------------------------------------------------------------------------
Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim Idx As Long, TempString As String
If KeyCode < 32 Or KeyCode > 126 Then Exit Sub
If KeyCode = vbKeyBack Then Exit Sub
TempString = Combo1.Text
Idx = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal TempString)
If Idx <> -1 Then
Combo1.ListIndex = Idx
Combo1.SelStart = Len(TempString)
Combo1.SelLength = Len(Combo1.Text) - Len(TempString)
Else
Combo1.Text = TempString
Combo1.SelStart = Len(TempString)
End If
End Sub
I know this is an old thread, but I'm hoping you can help me. I tried to use your code but I am getting a type mismatch on the "SendMessage" line at the combo1.hwnd. I have renamed the control to my control but all other code remains. It compiled ok, but I get the error when I type in the combobox. Any help is appreciated.
-
Dec 5th, 2017, 03:47 PM
#16
Re: combobox select as you type
can you create a sample project that demonstrates the problem?
zip and attach to post
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 5th, 2017, 04:02 PM
#17
New Member
Re: combobox select as you type
Originally Posted by westconn1
can you create a sample project that demonstrates the problem?
zip and attach to post
Thank you for responding. I actually ended up taking a different approach and got it working.
Thank you anyway,
Chris
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
|