Search in ComboBox of Style = 2 (dropdown list)

How to implement a search routine with only one call to a function or sub that will search the combobox for a string starting on the entered chars. After a while,it is resetted, and starts from zero again. Like in MS Explorer ?

It mus also responds as normally when you press the same character after each other, to go through all items starting on the entered first character. I have a LOT of comboboxes of Style = 2, and it is not realistic to enter code on a lot of different events.

Any other style than 2 is not possible, it should require to much code!

Any tips/code is very interesting
Thanks !

//Pgg
pggAsked:
Who is Participating?
 
ArkConnect With a Mentor Commented:
Hi
Change Comment to answer
Cheers
0
 
ArgonautCommented:
Do you want to find all possible matches in a list for text as it is entered?  If so MS Knowledgebase has a article how to do this on the KeyUp event.

If not please explan your question in more detail.
0
 
AzraSoundCommented:
theres a file you can download there called autofill.zip that contains the source code of doing something like this
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
AzraSoundCommented:
0
 
AzraSoundCommented:
heres the MS article Argonaut referred to:
http://support.microsoft.com/support/kb/articles/q154/0/76.asp?FR=0
0
 
ArkCommented:
Hi
All samples are for DropDown combo
For dropdown list it's not difficult:

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_FINDSTRING = &H14C

Private Function cboFindItem(cbo As ComboBox, sFind As String) As String
  Dim i As Long
  i = SendMessage(cbo.hwnd, CB_FINDSTRING, -1, ByVal sFind)
  If i <> -1 Then
     cboFindItem = cbo.List(i)
'uncomment following line if you want to change Combo index to find string
'     cbo.ListIndex = i
  End If
End Function

'Using: Place two text boxes and any number of comboboxes (style any, may be 2).

Private Sub Form_Load()
'Only to fill combos
  For i = 1 To 2
    For j = 1 To 9
       Combo1.AddItem "1" & CStr(i) & CStr(j)
       Combo2.AddItem "2" & CStr(i) & CStr(j)
       Combo3.AddItem "3" & CStr(i) & CStr(j)
    Next j
  Next i
End Sub

Private Sub Text1_Change()
  If Text1 <> "" Then
     Text2 = cboFindItem(Combo1, Text1.Text)
'or  Text2 = cboFindItem(Combo2, Text1.Text)
  End If
End Sub

Cheers
0
 
ArkCommented:
Hi
Another sample to find matches in all comboboxes

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_FINDSTRING = &H14C
Private Const CB_ERR = (-1)

Private Sub Form_Load()
  For i = 1 To 2
    For j = 1 To 9
       Combo1.AddItem "1" & CStr(i) & CStr(j)
       Combo2.AddItem "2" & CStr(i) & CStr(j)
       Combo3.AddItem "3" & CStr(i) & CStr(j)
    Next j
  Next i
End Sub

Private Function cboFindItem(cbo As ComboBox, sFind As String, nIndex As Long) As String
  Dim i As Long
  i = SendMessage(cbo.hwnd, CB_FINDSTRING, -1, ByVal sFind)
  If i <> -1 Then
     cboFindItem = cbo.List(i)
     nIndex = i
  End If
End Function

Private Sub Text1_Change()
  Dim ctl As Control, nTemp As Long
  If Text1 <> "" Then
     For Each ctl In Controls
       If TypeOf ctl Is ComboBox Then
          nTemp = -1
          Call cboFindItem(ctl, Text1.Text, nTemp)
          ctl.ListIndex = nTemp
       End If
     Next ctl
  End If
End Sub
0
 
pggAuthor Commented:
My needs is only to use the existing combobox, no textboxes etc. The user activates the combo, enter any text, and the combobox is opened, showing the first entry that matchs.

I have at least a couple of hundred comboboxes in my program, I dont have time or resourses to rewrite everything. Its a 8 mb exe file so the project is relative big.

the examples so far is working, but not the way I would like it to do !

//Pgg

0
 
ArkCommented:
Hi
How your user can type in combobox with style 2 (dropdownlist)?
Cheers
0
 
pggAuthor Commented:
Ok, maybe I must chage it to style = 1
0
 
ArkCommented:
Hi
In this case - which keyboard events for these combos (keydown,keyup,keypress) do you already use. (I need "free" event to make this code). If you use them all, I'm afraid, it's impossible.
Cheers
0
 
pggAuthor Commented:
Hi !

I dont use any of thoose events, only click !
And the style must be 0=normal combo box

I have tested the example in knowledgebase, as mentioned above, and I cant get it to work.

I am using VB5. It is complaining at the &Amp;
&gt>= things in the example. Probably because some features in VB6 that is not in VB5.

Thanks !

0
 
ArkCommented:
Hi
OK, say you have "free" KeyUp event.
1. Add Class module to your app (Class1)
'---- code for class module----
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_FINDSTRING = &H14C
Private WithEvents cbo As ComboBox

Public Property Set cboCtl(OutsideCombo As ComboBox)
  Set cbo = OutsideCombo
End Property

Private Sub cbo_KeyUp(KeyCode As Integer, Shift As Integer)
   Dim i As Long, sFind As String, sTotal As String
   If KeyCode = vbKeyDelete Or KeyCode = vbKeyBack Then Exit Sub
   With cbo
     sFind = .Text
     If sFind <> "" Then
        i = SendMessage(.hwnd, CB_FINDSTRING, -1, ByVal sFind)
        If i > -1 Then
           sTotal = .List(i)
           .ListIndex = i
           .SelStart = Len(sFind)
           .SelLength = Len(sTotal) - Len(sFind)
        End If
      End If
   End With
End Sub

'---Form code-----

'for all your combos
Private clsCombo1 As New Class1
Private clsCombo2 As New Class1
Private clsCombo3 As New Class1

Private Sub Form_Load()
'for all your combos
  Set clsCombo1.cboCtl = Combo1
  Set clsCombo2.cboCtl = Combo2
  Set clsCombo3.cboCtl = Combo3
'Fill combos (only for sample, you don't need it)
  For i = 1 To 3
    For j = 1 To 9
      Combo1.AddItem "1" & CStr(i) & CStr(j)
      Combo2.AddItem "2" & CStr(i) & CStr(j)
      Combo3.AddItem "3" & CStr(i) & CStr(j)
    Next j
  Next i
End Sub


' Private Sub Combo2_KeyUp(KeyCode As Integer, Shift As Integer)
' any your code - this event fire BEFORE class event. For example Text1=Text1 & "a" - You see search working and a printing at textbox
' End Sub

Cheers
0
 
pggAuthor Commented:
Ark, post a answer, it worked perfect after adding some more check on the possible keycode !!!

Thanks a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.