Solved

List boxes with search capabilities

Posted on 2000-03-16
6
217 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 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…

777 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