Solved

Search in ComboBox of Style = 2 (dropdown list)

Posted on 2000-03-20
14
194 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
  • 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
 
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 27

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 27

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

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 27

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 27

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 27

Accepted Solution

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Is an IP address in a Range 22 72
Access 2016 VB code 9 86
using Access 8 52
How to compare ms sql hashbytes results within vb6 5 37
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now