Here is my code the I wish can help you.
Option Explicit
Private mbIgnoreListClick As Boolean
Private Const CB_SHOWDROPDOWN = &H14F
Const CB_ERR = -1
Const CB_FINDSTRING = &H14C
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
Dim lReturn As Long
Dim id As Boolean
Dim keyid As Boolean
Dim key1 As Boolean
Private Sub Combo1_Click()
If key1 = False Then
Text1.Text = Combo1.List(Combo1.ListIndex)
Else
key1 = False
End If
End Sub
Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Or KeyCode = vbKeyDown Then
keyid = True
End If
End Sub
Private Sub Combo1_keypress(KeyAscii As Integer)
Dim sSearchText As String
SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 1, 0
If KeyAscii = 13 Then
key1 = True
SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, False, 0
If id = True And keyid = False Then
Combo1.Text = Combo1.List(lReturn)
Text1.Text = Combo1.List(lReturn)
Combo1.ListIndex = lReturn
MsgBox "list index: " & Combo1.ListIndex
Else
Combo1.Text = Combo1.List(Combo1.ListIndex)
Combo1_Click
keyid = False
End If
Else
sSearchText = Left$(Combo1.Text, Combo1.SelStart) &
Chr$(KeyAscii)
lReturn = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal
sSearchText)
If lReturn <> CB_ERR Then
mbIgnoreListClick = True
Combo1.ListIndex = lReturn
Combo1.Text = Combo1.List(lReturn)
Combo1.SelStart = Len(sSearchText)
Combo1.SelLength = Len(Combo1.Text)
KeyAscii = 0
id = True
End If
End If
End Sub
Private Sub Form_Load()
Combo1.AddItem "Apples"
Combo1.AddItem "Apbes"
Combo1.AddItem "Oranges"
Combo1.AddItem "Grapes"
Combo1.AddItem "Watermelons"
Combo1.AddItem "Grapefruit"
Combo1.AddItem "Rasberries"
End Sub
-----Original Message-----
From: Andy Irvine [mailto:andy@e...]
Sent: Thursday, September 13, 2001 9:19 AM
To: professional vb
Subject: [pro_vb] RE: Here's my auto-completing combobox sample code
Marco,
The point you make about a combo box being useless if the number of items is
too big to fit into it is very valid. In fact this is exactly the reason
why I wanted to use an auto-completion combo box. The users of my app will
have to select a name from a list of 200 hundred or so. Rather than make
them scroll through entire list, I thought would it would be nice if they
could find the right name by typing the first few letters in. The
autocomplete code pulls up the first name matching the letters they have
typed after each keystroke. They still have the option to use the mouse if
they want, in fact if they type in the first letter or two the code places
the highlight bar more or less in the right place for them.
Hope this makes sense,
Andy
P.S. I'm sure my code can be improved so if notice anything, I'd welcome the
feedback - cheers.
Subject: RE: Here's my auto-completing combobox sample code
From: Marco Straforini <marco.straforini@c...>
Date: Wed, 12 Sep 2001 10:08:03 -0700
X-Message-Number: 8
Andy,
thanks for sharing the code with us. I have only one question:
why do you need a name completion? There is already the drop
down menu that you can use to select an item, isn't that enough?
I can see the name completion useful only when the number of the
items is too big that they do not fit in a drop down menu, and
in that case the menu is useless.
I am not questioning, just wondering...
m.