[RESOLVED] check if combobox item contains a string
Hello guys. I would like to know if there is a code to check if a combobox item contains a string and then display only the combobox items that contain the string (like webbrowsers do with the adress combobox). I have found some solutions using instr but I would like someone to help me display these items in combobox (how to make them visible) Here is some code:
Code:
Private Sub Combo1_KeyPress(KeyAscii As Integer)
SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, True, ByVal 0
Dim i As Integer
For i = 0 To Combo1.ListCount - 1
'the code i need
Next i
End Sub
Re: check if combobox item contains a string
Re: check if combobox item contains a string
Thanks for your reply, but that's not what I'm saying. I want to have a code to display the combobox items i want. Have a look at the folowing code:
Code:
Option Explicit
Private Const CB_SHOWDROPDOWN = &H14F
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
'I have developed the code viewing by akhileshbc's links
Private Sub AdressBar_KeyDown(KeyCode As Integer, Shift As Integer)
Dim i As Integer
Dim Found
Select Case KeyCode
Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyShift, vbKeyEscape, vbKeyPageUp, vbKeyPageDown, vbKeyControl
Exit Sub
Case Else
SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, True, ByVal 0
For i = 0 To Combo1.ListCount - 1
found = InStr(1, AdressBar.List(i), AdressBar.Text, vbBinaryCompare)
If found <> 0 Then
'display these item
Next i
End Select
End Sub
Re: check if combobox item contains a string
To show only the matching items, remove everything that doesn't match.
To do that, replace this:
Code:
For i = 0 To Combo1.ListCount - 1
found = InStr(1, AdressBar.List(i), AdressBar.Text, vbBinaryCompare)
If found <> 0 Then
'display these item
Next i
...with this:
Code:
For i = Combo1.ListCount - 1 To 0 Step -1
found = InStr(1, AdressBar.List(i), AdressBar.Text, vbBinaryCompare)
If found = 0 Then
AdressBar.Remove i
End If
Next i
However, this will clearly reduce the amount of items each time - so if the amount of characters in AdressBar.Text reduces (or one or more of the characters in it are changed), you will need to re-load the list before re-running this loop.
Re: check if combobox item contains a string