Solved

List boxes with search capabilities

Posted on 2000-03-16
6
218 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

820 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