Solved

Search in ComboBox of Style = 2 (dropdown list)

Posted on 2000-03-20
14
210 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:pgg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 1

Expert Comment

by:Argonaut
ID: 2637498
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2637917
theres a file you can download there called autofill.zip that contains the source code of doing something like this
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2637920
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:AzraSound
ID: 2637925
heres the MS article Argonaut referred to:
http://support.microsoft.com/support/kb/articles/q154/0/76.asp?FR=0
0
 
LVL 28

Expert Comment

by:Ark
ID: 2638886
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
 
LVL 28

Expert Comment

by:Ark
ID: 2638909
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
 

Author Comment

by:pgg
ID: 2639270
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
 
LVL 28

Expert Comment

by:Ark
ID: 2639307
Hi
How your user can type in combobox with style 2 (dropdownlist)?
Cheers
0
 

Author Comment

by:pgg
ID: 2639357
Ok, maybe I must chage it to style = 1
0
 
LVL 28

Expert Comment

by:Ark
ID: 2639419
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
 

Author Comment

by:pgg
ID: 2639494
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
 
LVL 28

Expert Comment

by:Ark
ID: 2639534
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
 

Author Comment

by:pgg
ID: 2640406
Ark, post a answer, it worked perfect after adding some more check on the possible keycode !!!

Thanks a lot
0
 
LVL 28

Accepted Solution

by:
Ark earned 200 total points
ID: 2642318
Hi
Change Comment to answer
Cheers
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question