?
Solved

List boxes with search capabilities

Posted on 2000-03-16
6
Medium Priority
?
231 Views
Last Modified: 2010-05-27
I'm looking for a list box that when the user type in the first few letters of data, the list box will search thru its items and finish typing in the data. Is there code to do this? or a new component?
0
Comment
Question by:jalleman
[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 Comments
 
LVL 9

Expert Comment

by:Ruchi
ID: 2624772
0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2624775
Unless I'm much mistaken a standard listbox will attempt to match keypresses you hit, ie if your listbox contains the following items :

apple
banana
orange
pear
pineapple

pressing "p" will highlight "pear". press "i" straight after and it will select "pineapple".
0
 

Accepted Solution

by:
seeshelle earned 400 total points
ID: 2624797
I did some searching and found this on the net.
Make a bas file and paste this in it:
'-------------
'API Declares.
'=============

Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long

'--------------
'API Constants.
'==============

'Used for Hooking into the key board.
Public Const WH_KEYBOARD = 2

'Find a string.
Public Const CB_FINDSTRING = &H14C

'-----------------
'Public variables.
'=================

'Used for the windows hook.
Public hHook As Long

'Used to track the last key pressed.
Public LastKeyPressed As Long

'
'Used to handle keys pressed before windows gets to them.
'
Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If nCode >= 0 Then
        KeyboardProc = 1
        LastKeyPressed = wParam
    End If
    KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
End Function


Public Function AutoComplete(cbo As ComboBox) As Boolean
   
    Dim sText As String
    Dim lIndex As Long
    Static iLen As Integer
    Static bSearching As Boolean
   
    If bSearching Then Exit Function
   
    bSearching = True
   
    With cbo
       
        'Check if backspace was pressed.
        If LastKeyPressed = 8 Then
       
            'Backspace pressed so
            'strip off the last letter
            'of the combo text.
            If iLen = 0 Then iLen = 1
            .Text = Left$(.Text, iLen - 1)
            GoTo Clean_Exit
           
        End If
       
        'Check if delete was pressed.
        'Just exit and let the delete go ahead.
        If LastKeyPressed = 46 Then GoTo Clean_Exit
       
        sText = .Text
        iLen = Len(sText)

        'Used the SendMessage API for a fast search of the combo.
        lIndex = SendMessage(.hwnd, CB_FINDSTRING, -1, ByVal .Text)
   
        'Check if a match if found.
        If lIndex >= 0 Then
           
            'If so, select it.
            .ListIndex = lIndex
           
            'set the highlight text to
            'the auto completed section.
            .SelStart = iLen
            .SelLength = Len(.List(.ListIndex)) - iLen

            'Set the function to true.
            AutoComplete = True
   
        Else
           
            'Select nothing.
            .ListIndex = -1
           
            'Set the function to false.
            AutoComplete = False
   
        End If
       
    End With

Clean_Exit:

    bSearching = False
   
End Function

Then a simple form with a list box with this code:
Private Sub Form_Load()

    'Hook into the keyboard to track the key strokes.
    hHook = SetWindowsHookEx(WH_KEYBOARD, _
                             AddressOf KeyboardProc, _
                             0&, _
                             App.ThreadID)

    LoadList
   
End Sub

Private Sub cboTest_Change()
    AutoComplete cboTest
End Sub

Private Sub Form_Unload(Cancel As Integer)
   
   Call UnhookWindowsHookEx(hHook)
   
End Sub

Private Sub LoadList()
   
    'Load the combo with some test data.
    With cboTest
   
        .AddItem "Test"
        .AddItem "Another Test"
        .AddItem "Yet another test"
        .AddItem "Guess"
   
    End With

End Sub

There's lots of info out there if ya know where to look.
0
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.

 
LVL 9

Expert Comment

by:Ruchi
ID: 2624816
Very good site.
"Search for a String in a ListBox or ComboBox"
http://www.freevbcode.com/ShowCode.asp?ID=313&NoBox=True
0
 

Author Comment

by:jalleman
ID: 2624895
Adjusted points from 50 to 100
0
 

Author Comment

by:jalleman
ID: 2624896
Thanks all for the 'answers', wish i could give you all points. :)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 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