update combo dropdown list while typing

July 21st, 2012 - 08:58 pm ET by Eduardo | Report spam
I'm trying to achieve something similar like FireFox and other programs do
(Google in the search box), of making available on the combo dropdown list
just items that are relevant to what the user is typing.

I have several problems.

1) The mouse pointer disappears.
2) The list is not properly refreshed and the bottom part of the previous
list remains on the screen.
3) I don't know why the fist matching item is selected with the first
keystroke.

This is the code:

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

Private Const CB_GETDROPPEDSTATE As Long = &H157
Private Const CB_SHOWDROPDOWN As Long = &H14F

Private mList(9) As String

Private Sub Form_Load()
mList(0) = "aaa"
mList(1) = "aa"
mList(2) = "abbbba"
mList(3) = "lla"
mList(4) = "lfaa"
mList(5) = "adffkaaa"
mList(6) = "baa"
mList(7) = "bbba"
mList(8) = "baba"
mList(9) = "baa"
End Sub

Private Sub Combo1_Change()
If Trim (Combo1.Text) <> "" Then
FillCombo
If Not ComboIsDropped(Combo1) Then
If Combo1.ListCount > 0 Then
DropDownCombo Combo1
End If
End If
Else
DropUpCombo Combo1
End If
End Sub

Private Sub FillCombo()
Dim c As Long
Dim iStr As String

iStr = Trim (Combo1.Text)
Do Until Combo1.ListCount = 0
Combo1.RemoveItem 0
Loop
For c = 0 To UBound(mList)
If InStr(mList(c), iStr) > 0 Then
Combo1.AddItem mList(c)
End If
Next c
End Sub

Public Function ComboIsDropped(nCombo As ComboBox) As Boolean
ComboIsDropped = CBool(SendMessage(nCombo.hwnd, _
CB_GETDROPPEDSTATE, 0&, 0&))
End Function

Public Sub DropDownCombo(nCombo As ComboBox)
SendMessage nCombo.hwnd, CB_SHOWDROPDOWN, _
True, ByVal 0
End Sub

Public Sub DropUpCombo(nCombo As ComboBox)
SendMessage nCombo.hwnd, CB_SHOWDROPDOWN, _
False, ByVal 0
End Sub
email Follow the discussionReplies 18 repliesReplies Make a reply

Replies

#1 Eduardo
July 21st, 2012 - 10:37 pm ET | Report spam
"Eduardo" escribió en el mensaje
news:jufj6n$p35$
I'm trying to achieve something similar like FireFox and other programs do
(Google in the search box), of making available on the combo dropdown list
just items that are relevant to what the user is typing.

I have several problems.

1) The mouse pointer disappears.
2) The list is not properly refreshed and the bottom part of the previous
list remains on the screen.
3) I don't know why the first matching item is selected with the first
keystroke.



I fixed the 1) and 2), but I still don't like the way the 3) was "fixed":

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

Private Const CB_GETDROPPEDSTATE As Long = &H157
Private Const CB_SHOWDROPDOWN As Long = &H14F
Private Const CB_GETCOMBOBOXINFO As Long = &H164

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Type COMBOBOXINFO
cbSize As Long
rcItem As RECT
rcButton As RECT
stateButton As Long
hwndCombo As Long
hwndItem As Long
hwndList As Long
End Type

Private Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Private Const SW_HIDE As Long = 0
Private Const SW_SHOW As Long = 1

Private mList(9) As String

Private Sub Combo1_Change()
Dim iStr As String
Dim iSS As Long

If Trim(Combo1.Text) <> "" Then
FillCombo
If Not ComboIsDropped(Combo1) Then
If Combo1.ListCount > 0 Then
iSS = Combo1.SelStart
iStr = Combo1.Text
DropDownCombo Combo1
Combo1.Text = iStr
Combo1.SelStart = iSS
Combo1.MousePointer = 1
Combo1.MousePointer = 0
End If
End If
Else
DropUpCombo Combo1
End If
End Sub

Private Sub Form_Load()
mList(0) = "aaa"
mList(1) = "aa"
mList(2) = "abbbba"
mList(3) = "lla"
mList(4) = "lfaa"
mList(5) = "adffkaaa"
mList(6) = "baa"
mList(7) = "bbba"
mList(8) = "baba"
mList(9) = "baa"
End Sub

Private Sub FillCombo()
Dim c As Long
Dim iStr As String

If ComboIsDropped(Combo1) Then
ShowComboList Combo1, False
End If
iStr = Trim(Combo1.Text)
Do Until Combo1.ListCount = 0
Combo1.RemoveItem 0
Loop
For c = 0 To UBound(mList)
If InStr(mList(c), iStr) > 0 Then
Combo1.AddItem mList(c)
End If
Next c
If ComboIsDropped(Combo1) Then
ShowComboList Combo1, True
End If
End Sub

Public Function ComboIsDropped(nCombo As ComboBox) As Boolean
ComboIsDropped = CBool(SendMessage(nCombo.hwnd, _
CB_GETDROPPEDSTATE, 0&, 0&))
End Function

Public Sub DropDownCombo(nCombo As ComboBox)
SendMessage nCombo.hwnd, CB_SHOWDROPDOWN, _
True, ByVal 0
End Sub

Public Sub DropUpCombo(nCombo As ComboBox)
SendMessage nCombo.hwnd, CB_SHOWDROPDOWN, _
False, ByVal 0
End Sub

Private Sub ShowComboList(nCombo As ComboBox, _
nShow As Boolean)

Dim iCbInfo As COMBOBOXINFO

iCbInfo.cbSize = Len(iCbInfo)
SendMessage nCombo.hwnd, CB_GETCOMBOBOXINFO, _
0&, iCbInfo
ShowWindow iCbInfo.hwndList, Abs(CLng(nShow))
End Sub

Similar topics