Solved

Autofill a ComboBox

Posted on 1998-11-25
6
539 Views
Last Modified: 2013-12-26
I have browsed about 2000 questions here and finally got tired.  I need to autofill a combobox as the user types, with data from the list that matches what the user is typing.  
0
Comment
Question by:jjmartin
6 Comments
 
LVL 2

Expert Comment

by:trillo
Comment Utility
Something like the Windows help system (when you type a letter, the first items beggining with that letter are displayed) ??
0
 
LVL 7

Expert Comment

by:Inteqam
Comment Utility
ok ,
Where do you type , in a text box or something
?

0
 
LVL 1

Expert Comment

by:zivf
Comment Utility
there is no 'auto way' to do this.
1. empty the combo/list box (clear method)
2. fill it with the data that matches what you typed

you can use a collection of the objects that are the whole and insert (as you go through the collection with For Each) only the ones that match the current criteria (item Like txt & "*"). trying to do this with data from a database will produce poor results.
0
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.

 
LVL 7

Expert Comment

by:Inteqam
Comment Utility
any way , i think the whole needed thing is bad , and without any good results ,

Why do you want to display just those who match , why don't you stop at the first match ?

0
 
LVL 3

Accepted Solution

by:
vbWhiz earned 50 total points
Comment Utility
I also was once in search of the perfect VB combobox, one that would act like the combobox built into access. I stumbled across the source code for this one - so I can't take credit for the code, but it works great!

Sample Code:

'AutoFill - Automatic string fill demo
'Copyright (c) 1997 SoftCircuits Programming (R)
'Redistributed by Permission.
'
'This Visual Basic 5 example demonstrates how you can use a combo box
'to implement an "autofill" feature. Autofill attempts to enter the
'remainder of the string the user is typing. A combo box is used
'because they implement both a text box and a list. The code finds the
'first item in the list that begins with the text entered so far. It
'then appends the remainder of the matching list item to the text in
'the text box.
'
'The appended characters not typed by the user are highlighted. This
'way, any new keystrokes typed by the user automatically replace them,
'thereby not getting in the way of normal typing. Code is also added
'to make exceptions to certain keystrokes such as Delete and
'Backspace.
'
'This program may be distributed on the condition that it is
'distributed in full and unchanged, and that no fee is charged for
'such distribution with the exception of reasonable shipping and media
'charged. In addition, the code in this program may be incorporated
'into your own programs and the resulting programs may be distributed
'without payment of royalties.
'
'This example program was provided by:
' SoftCircuits Programming
' http://www.softcircuits.com
' P.O. Box 16262
' Irvine, CA 92623
Option Explicit

'Windows declarations
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const CB_FINDSTRING = &H14C
Private Const CB_ERR = (-1)

'Declarations for alternate code (see comments below)
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const CB_SETCURSEL = &H14E

'Private flag
Private m_bEditFromCode As Boolean


Private Sub Form_Load()
    Dim sSysDir As String, sFile As String

    'Get files from system directory for test list
    Screen.MousePointer = vbHourglass
    sSysDir = Space$(256)
    GetSystemDirectory sSysDir, Len(sSysDir)
    sSysDir = Left$(sSysDir, InStr(sSysDir, Chr$(0)) - 1)
    If Right$(sSysDir, 1) <> "\" Then
        sSysDir = sSysDir & "\"
    End If
    sFile = Dir$(sSysDir & "*.*")
    Do While Len(sFile)
        Combo1.AddItem sFile
        sFile = Dir$
    Loop
    Screen.MousePointer = vbDefault
End Sub

'Certain keystrokes must be handled differently by the Change
'event, so set m_bEditFromCode flag if such a key is detected
Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyDelete
            m_bEditFromCode = True
        Case vbKeyBack
            m_bEditFromCode = True
    End Select
End Sub

Private Sub Combo1_Change()
    Dim i As Long, j As Long
    Dim strPartial As String, strTotal As String

    'Prevent processing as a result of changes from code
    If m_bEditFromCode Then
        m_bEditFromCode = False
        Exit Sub
    End If
    With Combo1
        'Lookup list item matching text so far
        strPartial = .Text
        i = SendMessage(.hwnd, CB_FINDSTRING, -1, ByVal strPartial)
        'If match found, append unmatched characters
        If i <> CB_ERR Then
            'Get full text of matching list item
            strTotal = .List(i)
            'Compute number of unmatched characters
            j = Len(strTotal) - Len(strPartial)
            '
            If j <> 0 Then
                'Append unmatched characters to string
                m_bEditFromCode = True
                .SelText = Right$(strTotal, j)
                'Select unmatched characters
                .SelStart = Len(strPartial)
                .SelLength = j
            Else

                '*** Text box string exactly matches list item ***

                'Note: The ListIndex is still -1. If you want to
                'force the ListIndex to the matching item in the
                'list, uncomment the following line. Note that
                'PostMessage is required because Windows sets the
                'ListIndex back to -1 once the Change event returns.
                'Also note that the following line causes Windows to
                'select the entire text, which interferes if the
                'user wants to type additional characters.
'                PostMessage Combo1.hwnd, CB_SETCURSEL, i, 0
            End If
        End If
    End With
End Sub

Private Sub cmdClose_Click()
    Unload Me
End Sub

[-Note- Simply place a combobox on a form and copy this code onto the form. You can package all this into your own control if you don't like the idea of pasting this code everywhere you have a combobox]

0
 
LVL 3

Author Comment

by:jjmartin
Comment Utility
Thanks for the help, this worked perfectly.  Sorry to those that posted comments.  I didn't mean to be unclear, I was just in a hurry.
0

Featured Post

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).

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

763 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

13 Experts available now in Live!

Get 1:1 Help Now