Back


Public Class Form1
'/// your Form's Construction would be here.
'/// now to the bit we're interested in...

<DllImport("OLE32.DLL")> _
    Public Shared Function CoCreateInstance( _
         ByRef ClassGuid As Guid, _
         ByVal pUnkOuter As IntPtr, _
         ByVal dwClsContext As Integer, _
         ByRef InterfaceGuid As Guid, _
         ByRef Result As IAutoComplete) As IntPtr
    End Function

    Private CLSID_AutoComplete As New Guid("00BB2763-6A77-11D0-A535-00C04FD7D062")
    Private IID_IAutoComplete As New Guid("EAC04BC0-3791-11D2-BB95-0060977B464C")
    Private Const CLSCTX_INPROC_SERVER As Integer = 1

    Private autocom As IAutoComplete

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CoCreateInstance(CLSID_AutoComplete, IntPtr.Zero, CLSCTX_INPROC_SERVER, IID_IAutoComplete, autocom)
        If Not autocom Is Nothing Then
            Dim ienum As New EnumString
            ienum.strArray = ArrayList.Adapter(ListBox1.Items)
            autocom.Init(TextBox1.Handle, ienum, Nothing, Nothing)
            autocom.SetOptions(Convert.ToUInt32(ShowOptions.ACO_AUTOSUGGEST Or ShowOptions.ACF_UPDOWNKEYDROPSLIST Or ShowOptions.ACO_AUTOAPPEND))
            autocom.Enable(True)
        End If
    End Sub

End Class '/// end of the form's class

#Region " AutoComplete Class "
  <ComImport(), Guid("00BB2763-6A77-11D0-A535-00C04FD7D062")> _
Public Class AutoComplete
    '/// dummy to enable access to the IAutoComplete2 Interface
End Class
#End Region

#Region " IAutoComplete2 Interface "
  <ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("EAC04BC0-3791-11D2-BB95-0060977B464C")> _
Public Interface IAutoComplete

    Function Init(ByVal hwndEdit As IntPtr, <MarshalAs(UnmanagedType.IUnknown)> ByVal punkACL As Object, ByVal pwszRegKeyPath As String, ByVal pwszQuickComplete As String) As Int32
    '/// we must use <PreserveSig()> _ otherwise after using autocomplete , fields will not return the next time
    <PreserveSig()> _
    Function Enable(<[In]()> ByVal fEnable As Boolean) As Boolean
    <PreserveSig()> _
    Function SetOptions(<[In]()> ByVal dwFlag As UInt32) As Integer
    <PreserveSig()> _
    Function GetOptions(<Out()> ByRef pdwFlag As UInt32) As Integer

End Interface

#End Region


 
#Region " IEnumString Interface "
  '/// HKEY_CLASSES_ROOT\Interface\{00000101-0000-0000-C000-000000000046}\IEnumString
<ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000101-0000-0000-C000-000000000046")> _
Public Interface IEnumString

    Function [Next](ByVal celt As Integer, ByVal rgelt() As String, ByRef pceltFetched As Integer) As Integer
    Function Skip(ByVal celt As Integer) As Integer
    Function Reset() As Integer
    Sub Clone(ByRef ppenum As IEnumString)

End Interface

#End Region

 
#Region " IEnumString Class "
  Public Class EnumString
    '/// UCOMIEnumString is the same as the IEnumString interface
    '/// see more at Msdn on http://msdn.microsoft.com/library/d...mi_d2l_89uv.asp
    Implements UCOMIEnumString

    Public strArray As ArrayList
    Private Pos As Integer = 0

    Public Function [Next](ByVal celt As Integer, ByVal rgelt As String(), ByRef pceltFetched As Integer) As Integer Implements UCOMIEnumString.Next
        Dim retval As Integer = 1
        pceltFetched = 0
        While Not Pos = strArray.Count AndAlso Not pceltFetched = celt
            rgelt(pceltFetched) = strArray(Pos)
            pceltFetched += 1
            Pos += 1
        End While

        If Not pceltFetched.CompareTo(celt) = -1 Then
            retval = 0
        End If

        Return retval

    End Function

    Public Function Skip(ByVal celt As Integer) As Integer Implements UCOMIEnumString.Skip
        Dim retval As Integer = 1
        Pos += celt

        If Not Pos = strArray.Count Then
            retval = 0
        End If

        Return retval
    End Function

    Public Function Reset() As Integer Implements UCOMIEnumString.Reset
        Pos = 0
        Return Pos
    End Function


    Public Sub Clone(ByRef ppenum As UCOMIEnumString) Implements UCOMIEnumString.Clone
        '/// create a Clone of this Class
        ppenum = DirectCast(Me, EnumString)
    End Sub

End Class

#End Region

#Region " AutoComplete Show Options Enums "
  Public Enum ShowOptions
    ACO_NONE = 0
    ACO_AUTOSUGGEST = &H1
    ACO_AUTOAPPEND = &H2
    ACO_SEARCH = &H4
    ACO_FILTERPREFIXES = &H8
    ACO_USETAB = &H10
    ACF_UPDOWNKEYDROPSLIST = &H20
    ACO_RTLREADING = &H40
End Enum
#End Region