dcsimg
Results 1 to 17 of 17
  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    162

    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.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,604

    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

  3. #3
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    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
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    162

    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.

  5. #5
    Lively Member
    Join Date
    Mar 2009
    Posts
    66

    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    162

    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    162

    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.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    162

    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.

  9. #9
    Lively Member
    Join Date
    Mar 2009
    Posts
    66

    Re: combobox select as you type

    hmmm.. that's odd.. the code is working perfectly to me...

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    162

    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.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,604

    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    162

    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.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    162

    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

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,604

    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

  15. #15
    New Member
    Join Date
    Dec 2017
    Posts
    2

    Re: combobox select as you type

    Quote Originally Posted by __wired__ View Post
    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.

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,604

    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

  17. #17
    New Member
    Join Date
    Dec 2017
    Posts
    2

    Re: combobox select as you type

    Quote Originally Posted by westconn1 View Post
    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
  •  



Featured


Click Here to Expand Forum to Full Width