Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Search in ComboBox of Style = 2 (dropdown list)

Posted on 2000-03-20
14
Medium Priority
?
227 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 800 total points
ID: 2642318
Hi
Change Comment to answer
Cheers
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

610 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