Link to home
Start Free TrialLog in
Avatar of JXCovert
JXCovertFlag for United States of America

asked on

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.

Capisce?

Please help me.  <wink>

John
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Avatar of JXCovert

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tinkledoomhammer
tinkledoomhammer

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

#Else

   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()
 'JUST SAMPLE FOR DEMO PURPOSES
 'YOU CAN OBVIOUSLY ADD WHAT YOU NEED
 'TO ADD HERE
    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