Results 1 to 6 of 6

Thread: AutoComplete in Combo Box

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833

    AutoComplete in Combo Box

    I want to create a combo box where when you type into the box it searches the combo list and then automatically fills in the text but highlights it... just like the IE address box.

    I can't get the seltext to work properly

    c is my combo box
    -------------------------------------------------


    On Error Resume Next

    For a = 0 To 2

    If Left$(c.Text, Len(c.Text)) = Left$(c.List(a), Len(c.Text)) Then


    If c.Text <> c.List(a) Then
    c.Text = c.List(a)
    End If

    b = Len(c.Text)

    c.SelStart = b

    c.SelLength = Len(c.Text) - b + 1

    Exit Sub



    End If

    Next a
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. Private Backspaced As Boolean
    2.  
    3.  
    4. Private Sub Form_Load()
    5.     'Drop some test items into the combo
    6.     With cboAuto
    7.     .AddItem "http://www.vbthunder.com"
    8.     .AddItem "http://www.mvps.org/vb/"
    9.     .AddItem "http://www.mvps.org/vbnet/"
    10.     .AddItem "http://members.aol.com/btmtz/vb/index.htm"
    11.     .AddItem "http://www.zonecorp.com"
    12.     .AddItem "http://www.microsoft.com"
    13.     .AddItem "http://www.mvps.org/ccrp/"
    14.     End With
    15. End Sub
    16.  
    17. Private Sub cboAuto_KeyDown(KeyCode As Integer, Shift As Integer)
    18.  
    19.     If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
    20.         If cboAuto.Text <> "" Then
    21.             'Let the Change event know that it
    22.             'shouldn't respond to this change.
    23.             Backspaced = True
    24.         End If
    25.     End If
    26.  
    27. End Sub
    28.  
    29. Private Sub cboAuto_Change()
    30.  
    31.     'If firing in response to a backspace
    32.     'or delete, don't run the auto-complete
    33.     'complete code. (Otherwise you wouldn't
    34.     'be able to back up.)
    35.     If Backspaced = True Or cboAuto.Text = "" Then
    36.         Backspaced = False
    37.         Exit Sub
    38.     End If
    39.  
    40.     Dim i As Long
    41.     Dim nSel As Long
    42.     'Run through the available items and
    43.     'grab the first matching one.
    44.     For i = 0 To cboAuto.ListCount - 1
    45.         If InStr(1, cboAuto.List(i), cboAuto.Text, _
    46.         vbTextCompare) = 1 Then
    47.             'Save the SelStart property.
    48.             nSel = cboAuto.SelStart
    49.             cboAuto.Text = cboAuto.List(i)
    50.             'Set the selection in the combo.
    51.             cboAuto.SelStart = nSel
    52.             cboAuto.SelLength = Len(cboAuto.Text) - nSel
    53.             Exit For
    54.         End If
    55.     Next
    56.  
    57. End Sub

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833
    THANKS!

    I was close...

    I needed to make sure the process only can when the combo box was actually typed in rather than had its value set

    I didn't want this to fire the event

    cbo.text = "http://"


    I added a

    private KeyHasBeenPressed as boolean

    so i set its value to true in the keydown
    and false in the change and keyup event

    sound good?

    thanks
    again!
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  4. #4
    Addicted Member djengiz's Avatar
    Join Date
    Jan 2001
    Location
    The Netherlands
    Posts
    153

    Hi!

    I created some code that does an autocomplete for a combobox, but it is all database driven. You can even add records via the combobox. You want to see the code? It is rather large......

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833
    No, my lists come from text files and they are pretty short.

    If anything I will add a better search routine.

    I actually was about to set out an upgrade my project... I quickly added this this morning

    so 4 hours later about 300 people are seeing it in action and I have an install of another 50 users this weekend

    thanks
    - kurt
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  6. #6
    Staifour
    Guest
    i see that all is done
    but i wanted to say that maybe it'll be easier to program it if you include the LIKE statement in there

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