Chan,
Thanks very much for the code - very interesting. I like it a lot because
is more compact than my version. I have modified your Combo_keypress event
code to stop the user adding a character to the combobox which doesn't
correspond to an entry in the list. I also have removed the Text box as I
don't need it to display the result.
The only thing I prefer about my original code over my modfication to your
code (shown below), is that in my version the character that user types is
shown appended to the combo1.text while the key is down. On Key up if the
new text doesn't correspond to a list item then the combo reverts back to
how it was before. Whereas, in my modification to your code, the 'invalid'
key never appears, which could give the user a frozen keyboards sensation.
(This probably won't make sense unless your run the codes and
experiment...).
example,
[<> indicates highlight portion of text]
Combo1.text Watermel<on>
Keystroke 'y'
My code My modification to your code
Display:
KeyDown Watermely Watermel<on>
KeyUp Watermel<on> Watermel<on>
I've tried to repeat this behaviour using your approach, but have failed.
So I'm going to stay with my original code for the time being. Anyway,
thanks again for sharing your code.
Andy
-----------------------------------------------------------
ption Explicit
Private mblnInvalid 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_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
mblnInvalid = False
SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 1, 0
'if user hits return the drop down combo and set .text
If KeyAscii = 13 Then
SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, False, 0
If id = True And keyid = False Then
Combo1.Text = Combo1.List(lReturn)
Combo1.ListIndex = lReturn
'MsgBox "list index: " & Combo1.ListIndex
Else
Combo1.Text = Combo1.List(Combo1.ListIndex)
keyid = False
End If
Else
'append key hit to current .text and return it's .listindex if found
sSearchText = Left$(Combo1.Text, Combo1.SelStart) & Chr$(KeyAscii)
lReturn = SendMessage(Combo1.hwnd, CB_FINDSTRING, -1, ByVal sSearchText)
'string is found in list
If lReturn <> CB_ERR Then
Combo1.ListIndex = lReturn
Combo1.Text = Combo1.List(lReturn)
Combo1.SelStart = Len(sSearchText)
Combo1.SelLength = Len(Combo1.Text)
id = True
End If
'SET THE KEY TO VBNULLSTRING IF NOT BACKSPACE
'*********************************************
If Not KeyAscii = vbKeyBack Then KeyAscii = 0
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
----------------------------------------------------------------------
Subject: RE: Here's my auto-completing combobox sample code
From: Chan Khin Sin <kschan@s...>
Date: Thu, 13 Sep 2001 00:58:39 -0500
X-Message-Number: 12
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