|
-
Feb 28th, 2002, 07:32 AM
#1
Thread Starter
Fanatic Member
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!]
-
Feb 28th, 2002, 07:36 AM
#2
VB Code:
Private Backspaced As Boolean
Private Sub Form_Load()
'Drop some test items into the combo
With cboAuto
.AddItem "http://www.vbthunder.com"
.AddItem "http://www.mvps.org/vb/"
.AddItem "http://www.mvps.org/vbnet/"
.AddItem "http://members.aol.com/btmtz/vb/index.htm"
.AddItem "http://www.zonecorp.com"
.AddItem "http://www.microsoft.com"
.AddItem "http://www.mvps.org/ccrp/"
End With
End Sub
Private Sub cboAuto_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
If cboAuto.Text <> "" Then
'Let the Change event know that it
'shouldn't respond to this change.
Backspaced = True
End If
End If
End Sub
Private Sub cboAuto_Change()
'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 Backspaced = True Or cboAuto.Text = "" Then
Backspaced = False
Exit Sub
End If
Dim i As Long
Dim nSel As Long
'Run through the available items and
'grab the first matching one.
For i = 0 To cboAuto.ListCount - 1
If InStr(1, cboAuto.List(i), cboAuto.Text, _
vbTextCompare) = 1 Then
'Save the SelStart property.
nSel = cboAuto.SelStart
cboAuto.Text = cboAuto.List(i)
'Set the selection in the combo.
cboAuto.SelStart = nSel
cboAuto.SelLength = Len(cboAuto.Text) - nSel
Exit For
End If
Next
End Sub
-
Feb 28th, 2002, 07:49 AM
#3
Thread Starter
Fanatic Member
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!]
-
Feb 28th, 2002, 08:41 AM
#4
Addicted Member
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......
-
Feb 28th, 2002, 12:49 PM
#5
Thread Starter
Fanatic Member
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!]
-
Feb 28th, 2002, 01:02 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|