Results 1 to 4 of 4

Thread: [RESOLVED] Combo box auto complete (and the ability to correct typos)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Resolved [RESOLVED] Combo box auto complete (and the ability to correct typos)

    I am using the code by Comintern in the following link to auto complete a combo box

    http://www.vbforums.com/showthread.p...ight=Combo+Box

    The code works beautifully except that if I type the letter J instead of K it will not allow me to backspace. Anybody have any ideas how to fix this.

    I really have not had a change to figure something out yet.

    Thank You

  2. #2
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Combo box auto complete (and the ability to correct typos)

    This is the code I use, see if it works for you:
    Code:
    Option Explicit
    Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
                                                                               ByVal wMsg As Long, _
                                                                               ByVal wParam As Long, _
                                                                               ByVal lParam As String) _
                                                                               As Long
    Private Const CB_FINDSTRING = &H14C
    Private bBkSpOrDel As Boolean
    
    Private Sub Form_Activate()
        
        Dim i As Integer, j As Integer, k As Integer, l As Integer
        Dim s As String, t As String
        
        Randomize
        
        For i = 1 To 4000
        
            k = 1 + 10 * Rnd
            
            s = ""
            
            For j = 1 To k
            
                l = 97 + 25 * Rnd
                t = Chr(l)
                s = s & t
                
            Next
            
            Combo1.AddItem s
        Next
        
    End Sub
    
    Private Sub Combo1_Change()
    
        Dim lRet      As Long
        Dim lSelStart As Long
    
        If bBkSpOrDel Then Exit Sub
    
        With Combo1
            lRet = SendMessageStr(.hwnd, CB_FINDSTRING, 0, .Text)
            lSelStart = .SelStart
            If lRet >= 0 Then
                .ListIndex = lRet
                .SelStart = lSelStart
                .SelLength = Len(.Text)
            End If
        End With
    
    End Sub
    
    Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
    
        Select Case KeyCode
            Case vbKeyDelete, vbKeyBack
                bBkSpOrDel = True
            Case Else
                bBkSpOrDel = False
        End Select
    
    End Sub
    Last edited by krtxmrtz; Nov 21st, 2007 at 03:17 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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

    Re: Combo box auto complete (and the ability to correct typos)

    Another way
    vb Code:
    1. Option Explicit
    2.  
    3. Private blnBackSpace As Boolean
    4.  
    5. Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
    6.  If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
    7.         If Combo1.Text <> vbNullString Then
    8.           'Let the Change event know that it shouldn't respond to this change.
    9.           blnBackSpace = True
    10.         End If
    11.     End If
    12. End Sub
    13.  
    14. Private Sub Combo1_Change()
    15.     Dim i As Long
    16.     Dim lngText As Long
    17.     'If firing in response to a backspace or delete, don't run the auto-complete
    18.     'complete code. (Otherwise you wouldn't be able to back up.)
    19.     If blnBackSpace = True Or Combo1.Text = vbNullString Then
    20.         blnBackSpace = False
    21.         Exit Sub
    22.     End If
    23.  
    24.     'Run through the available items and grab the first matching one.
    25.     For i = 0 To Combo1.ListCount - 1
    26.         If InStr(1, Combo1.List(i), Combo1.Text, vbTextCompare) = 1 Then
    27.             'Save the SelStart property.
    28.             lngText = Combo1.SelStart
    29.             Combo1.Text = Combo1.List(i)
    30.             'Set the selection in the combo.
    31.             Combo1.SelStart = lngText
    32.             Combo1.SelLength = Len(Combo1.Text) - lngText
    33.             Exit For
    34.         End If
    35.     Next
    36. End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    196

    Re: Combo box auto complete (and the ability to correct typos)

    Thank you Hack.

    That is fairly close to what I want. That will do for now

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