Solved

List boxes with search capabilities

Posted on 2000-03-16
6
221 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 100 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
Technology Partners: 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 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

707 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