• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1122
  • Last Modified:

Autofill a textbox in VB6

We have all seen (well at least those who have graduated to the Nineties with Win2000 and up) the "autofill" features in text boxes.  Most notably one finds them in Outlook, where you can begin typing an email address or contact name and if you have sent to that person before, Outlook gives you a list of previous entries that you can choose from.

Does anyone know how to implement this?

I have seen code snippets out there but they are almost all combobox-related.  I'd like something that works just like in Outlook, with the textbox dropping down into a mini listbox with no UI around it.


Please help me.  <wink>

1 Solution
Éric MoreauSenior .Net ConsultantCommented:
JXCovertAuthor Commented:
That is good stuff but I don't want URLs or drives....

I need to be able to draw from my own list in effect.
Éric MoreauSenior .Net ConsultantCommented:
It is essentially the same whether you use an edit box or any other control.
If your control doesn't maintain a list for you, you have to do it yourself. A simple linked list is ideal. Initially it contains all previously entered data.
With each keystroke, you search through the list, and delete any inappropriate entries. That does mean that you have to handle keystrokes.

Filling in the text is simple... just replace it with SetWindowText and move the cursor.

I can't give you an example because i don't do vb. If you _want_ one in c++ i could make one, though.
here's an example:
setup a form and add a combo box (Combo1)
paste in the following...

Option Explicit

#If Win32 Then

   Private Declare Function SendMessage Lib "user32" Alias _
       "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
       ByVal wParam As Long, lParam As Long) As Long


   Private Declare Function SendMessage Lib "User" ( _
       ByVal hwnd As Integer, ByVal wMsg As Integer, _
       ByVal wParam As Integer, lParam As Any) As Long

#End If

Private Const WM_SETREDRAW = &HB
Private msOldString As String ' module level global
Private miStart As Integer    ' module level global
Private miLength As Integer   ' module level global

 Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)
    Dim sComboText As String
    Dim iLoop As Integer
    Dim sTempString As String
    Dim lReturn As Long
    Dim bInList As Boolean
    Dim sItem
  If Not KeyCode = Asc(vbTab) And Not KeyCode = vbKeyShift And _
      Not KeyCode = vbKeyLeft And Not KeyCode = vbKeyRight And _
      Not KeyCode = vbKeyHome And Not KeyCode = vbKeyEnd Then
        bInList = False
        With Combo1
            sTempString = .Text
            If Len(sTempString) = 1 Then sComboText = sTempString
            lReturn = SendMessage(.hwnd, WM_SETREDRAW, False, 0&)
            For iLoop = 0 To (.ListCount - 1)
                sItem = .List(iLoop)
                If UCase((sTempString & Mid$(sItem, _
                 Len(sTempString) + 1))) = UCase(sItem) Then
                    .ListIndex = iLoop
                    .Text = sItem
                    msOldString = sItem
                    miStart = Len(sTempString)
                    .SelStart = miStart
                    miLength = Len(sItem) - (Len(sTempString))
                    .SelLength = miLength
                    sComboText = sComboText & Mid$(sTempString, _
                        Len(sComboText) + 1)
                    bInList = True
                    Exit For
                End If
            Next iLoop
            If Not bInList Then
                .Text = msOldString
                .SelStart = miStart
                .SelLength = miLength
            End If
            lReturn = SendMessage(.hwnd, WM_SETREDRAW, True, 0&)
        End With
    End If
   End Sub

Private Sub Form_Load()
    With Combo1
        .AddItem "Blue"
        .AddItem "Green"
        .AddItem "Yellow"
        .ListIndex = 0
        .Text = .List(0)
        .SelStart = 0
        .SelLength = Len(.Text)
        msOldString = .Text
        miStart = 0
        miLength = .SelLength
    End With
End Sub


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now