Implementing the IAutoComplete2 Interface in Vb.Net-VBForums
Results 1 to 1 of 1

Thread: Implementing the IAutoComplete2 Interface in Vb.Net

  1. #1

    Thread Starter
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,140

    Implementing the IAutoComplete2 Interface in Vb.Net

    I've been teaching myself a bit about Interfaces for a little while and decided to try to create a vb.net version of the IAutoComplete2 interface.
    you can see info on this for C++ on msdn.
    this example implements autocomplete on an given array of strings ( eg: in a listbox , combobox etc... )
    VB Code:
    1. Private autoComp As AutoComplete
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         '/// add autocomlete to an array of strings, in this case a listbox
    5.         autoComp = New AutoComplete(TextBox1.Handle, ArrayList.Adapter(ListBox1.Items))
    6.         '/// enable autocomplete
    7.         autoComp.Enable()
    8.  
    9.     End Sub
    10. End Class
    11.  
    12. #Region " AutoComplete Class "
    13.  
    14. Public Class AutoComplete
    15.     Implements IDisposable
    16.  
    17.     Private autoComp As Type
    18.     Private objAutoComp As IAutoComplete
    19.  
    20.     Public Sub New(ByVal hwndEdit As IntPtr, ByVal strList As ArrayList, Optional ByVal pwszRegKeyPath As String = Nothing, Optional ByVal pwszQuickComplete As String = Nothing)
    21.         Dim iEnumList As New EnumString()
    22.         iEnumList.strArray = strList
    23.  
    24.         autoComp = Type.GetTypeFromCLSID(New Guid("{00BB2763-6A77-11D0-A535-00C04FD7D062}"))
    25.         objAutoComp = DirectCast(Activator.CreateInstance(autoComp), IAutoComplete)
    26.  
    27.         '/// initialize IAutoComplete
    28.         objAutoComp.Init(hwndEdit, iEnumList, pwszRegKeyPath, pwszQuickComplete)
    29.         '/// set IAutoComplete Options
    30.         objAutoComp.SetOptions(Convert.ToUInt32(ShowOptions.ACO_AUTOSUGGEST Or ShowOptions.ACF_UPDOWNKEYDROPSLIST Or ShowOptions.ACO_AUTOAPPEND))
    31.         '/// Enable IAutoComplete
    32.         objAutoComp.Enable(True)
    33.  
    34.     End Sub
    35.  
    36.     Public Sub Dispose() Implements System.IDisposable.Dispose
    37.         Me.ReleaseComObject = Marshal.ReleaseComObject(objAutoComp)
    38.     End Sub
    39.  
    40.     Private WriteOnly Property ReleaseComObject()
    41.         Set(ByVal Value)
    42.             Me.ReleaseComObject = Value
    43.         End Set
    44.     End Property
    45.  
    46.     Public Sub Disable()
    47.         objAutoComp.SetOptions(Convert.ToUInt32(ShowOptions.ACO_NONE))
    48.     End Sub
    49.  
    50.     Public Sub Enable()
    51.         objAutoComp.SetOptions(Convert.ToUInt32(ShowOptions.ACO_AUTOSUGGEST Or ShowOptions.ACF_UPDOWNKEYDROPSLIST Or ShowOptions.ACO_AUTOAPPEND))
    52.     End Sub
    53.  
    54. End Class
    55.  
    56. #End Region
    57.  
    58.  
    59. #Region " IAutoComplete2 Interface "
    60.  
    61. <ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("EAC04BC0-3791-11D2-BB95-0060977B464C")> _
    62. Public Interface IAutoComplete
    63.  
    64.     Function Init(ByVal hwndEdit As IntPtr, <MarshalAs(UnmanagedType.IUnknown)> ByVal punkACL As Object, ByVal pwszRegKeyPath As String, ByVal pwszQuickComplete As String) As Int32
    65.     '/// we must use <PreserveSig()> _ otherwise after using autocomplete , fields will not return the next time
    66.     <PreserveSig()> _
    67.     Function Enable(<[In]()> ByVal fEnable As Boolean) As Boolean
    68.     <PreserveSig()> _
    69.     Function SetOptions(<[In]()> ByVal dwFlag As UInt32) As Integer
    70.     <PreserveSig()> _
    71.     Function GetOptions(<Out()> ByRef pdwFlag As UInt32) As Integer
    72.  
    73. End Interface
    74.  
    75. #End Region
    76.  
    77. #Region " IEnumString Interface "
    78. '/// HKEY_CLASSES_ROOT\Interface\{00000101-0000-0000-C000-000000000046}\IEnumString
    79. <ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000101-0000-0000-C000-000000000046")> _
    80. Public Interface IEnumString
    81.  
    82.     Function [Next](ByVal celt As Integer, ByVal rgelt() As String, ByRef pceltFetched As Integer) As Integer
    83.     Function Skip(ByVal celt As Integer) As Integer
    84.     Function Reset() As Integer
    85.     Sub Clone(ByRef ppenum As IEnumString)
    86.  
    87. End Interface
    88.  
    89. #End Region
    90.  
    91. #Region " IEnumString Class "
    92.  
    93. Public Class EnumString
    94.     '/// UCOMIEnumString is the same as the IEnumString interface
    95.     '/// see more at Msdn on [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmi_d2l_89uv.asp[/url]
    96.     Implements UCOMIEnumString
    97.  
    98.     Public strArray As ArrayList
    99.     Private Pos As Integer = 0
    100.  
    101.     Public Function [Next](ByVal celt As Integer, ByVal rgelt As String(), ByRef pceltFetched As Integer) As Integer Implements UCOMIEnumString.Next
    102.         Dim retval As Integer = 1
    103.         pceltFetched = 0
    104.         While Not Pos = strArray.Count AndAlso Not pceltFetched = celt
    105.             rgelt(pceltFetched) = strArray(Pos)
    106.             pceltFetched += 1
    107.             Pos += 1
    108.         End While
    109.  
    110.         If Not pceltFetched.CompareTo(celt) = -1 Then
    111.             retval = 0
    112.         End If
    113.  
    114.         Return retval
    115.  
    116.     End Function
    117.  
    118.     Public Function Skip(ByVal celt As Integer) As Integer Implements UCOMIEnumString.Skip
    119.         Dim retval As Integer = 1
    120.         Pos += celt
    121.  
    122.         If Not Pos = strArray.Count Then
    123.             retval = 0
    124.         End If
    125.  
    126.         Return retval
    127.     End Function
    128.  
    129.     Public Function Reset() As Integer Implements UCOMIEnumString.Reset
    130.         Pos = 0
    131.         Return Pos
    132.     End Function
    133.  
    134.  
    135.     Public Sub Clone(ByRef ppenum As UCOMIEnumString) Implements UCOMIEnumString.Clone
    136.         '/// create a Clone of this Class
    137.         ppenum = DirectCast(Me, EnumString)
    138.     End Sub
    139.  
    140. End Class
    141.  
    142. #End Region
    143.  
    144. #Region " AutoComplete Show Options Enums "
    145.  
    146. Public Enum ShowOptions
    147.     ACO_NONE = 0
    148.     ACO_AUTOSUGGEST = &H1
    149.     ACO_AUTOAPPEND = &H2
    150.     ACO_SEARCH = &H4
    151.     ACO_FILTERPREFIXES = &H8
    152.     ACO_USETAB = &H10
    153.     ACF_UPDOWNKEYDROPSLIST = &H20
    154.     ACO_RTLREADING = &H40
    155. End Enum
    156.  
    157. #End Region
    included as always a sample project...
    Attached Files Attached Files
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.