AutoComplete (TextBox)


Description:
This class adds autocomplete functionality to a textbox. It requires a collection of strings that it uses to provide the autocomplete text, and the textbox to add the functionality to. See the sample usage at the end of the page for an example of how to use this class.
 
Code:
'----- Class: clsACText

Option Explicit

Private m_col As Collection
Private WithEvents m_txt As TextBox
Private m_bEnabled As Boolean
Private m_bLastKeyDel As Boolean

Public Property Get Enabled() As Boolean
    Enabled = m_bEnabled
End Property

Public Property Let Enabled(bNew As Boolean)
    Debug.Assert bNew And Not (m_col Is Nothing)
    Debug.Assert bNew And Not (m_txt Is Nothing)
    m_bEnabled = bNew
End Property

Public Property Get TextBox() As TextBox
    Set TextBox = m_txt
End Property

Public Property Set TextBox(txtNew As TextBox)
    Set m_txt = txtNew
End Property

Public Property Get Collection() As Collection
    Set Collection = m_col
End Property

Public Property Set Collection(colNew As Collection)
    Set m_col = colNew
End Property

Public Sub ResortCollection()
    
    Debug.Assert Not (m_col Is Nothing)
    If m_col.Count <= 1 Then
        Exit Sub
    End If
    
    Dim i As Long
    Dim j As Long
    Dim nGap As Long
    Dim bResult As Boolean
    
    Dim tmp
    Dim tmp2
    
    nGap = m_col.Count / 2
    Do While nGap > 0
        
        For i = nGap To m_col.Count - 1
            
            tmp = m_col(i + 1)
            j = i
            
            bResult = (StrComp(tmp, m_col(j - nGap + 1),  _
                    vbBinaryCompare) = -1)
            
            Do While j >= nGap And bResult
                
                tmp2 = m_col(j - nGap + 1)
                m_col.Remove j + 1
                If j + 1 > m_col.Count Then
                    m_col.Add tmp2
                Else
                    m_col.Add tmp2, , j + 1
                End If
                
                j = j - nGap
                If j >= nGap Then
                    bResult = (StrComp(tmp, m_col(j - nGap + 1),  _
                            vbBinaryCompare) = -1)
                End If
            Loop
            
            m_col.Remove j + 1
            If j + 1 > m_col.Count Then
                m_col.Add tmp
            Else
                m_col.Add tmp, , j + 1
            End If
            
        Next
        
        nGap = nGap / 2
    Loop
    
End Sub

Private Sub m_txt_Change()
    
    If Not m_bEnabled Or m_bLastKeyDel Then
        Exit Sub
    End If
    
    If m_txt.SelStart <> Len(m_txt.Text) Then
        Exit Sub
    End If
    
    If m_txt.Text = "" Then
        Exit Sub
    End If
    
    Dim vItem As Variant
    Dim sContain As String
    
    sContain = LCase(m_txt.Text)
    
    For Each vItem In m_col
        If Mid(LCase(vItem), 1, Len(sContain)) = sContain Then
            m_txt = m_txt & Mid(vItem, Len(sContain) + 1)
            m_txt.SelStart = Len(sContain)
            m_txt.SelLength = Len(m_txt) - Len(sContain)
            Exit For
        End If
    Next
    
End Sub

Private Sub m_txt_KeyDown(KeyCode As Integer, Shift As Integer)
    
    If Not m_bEnabled Then
        Exit Sub
    End If
    
    If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
        m_bLastKeyDel = True
    Else
        m_bLastKeyDel = False
    End If
    
End Sub

'----- End of class
 
Sample Usage:
 
    Set m_acText = New clsACText    'Create the class
    Set m_acText.Collection = m_col 'Point the class to the collection
    Set m_acText.TextBox = Me.Text1 ' and the TextBox
    m_acText.ResortCollection       'Resort the collection to
                                    ' make things faster
    m_acText.Enabled = True         'And enable it