Solved

List boxes with search capabilities

Posted on 2000-03-16
6
214 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

19 Experts available now in Live!

Get 1:1 Help Now