Imports System.Runtime.InteropServices
Public Class XTipComboBox
Inherits ComboBox
Private Const CB_FINDSTRING As Integer = &H14C
Private Const CB_ERR As Integer = -1
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function
Protected Overrides Sub OnTextChanged(e As EventArgs)
MyBase.OnTextChanged(e)
AutoCompleteItems()
End Sub
Private Sub AutoCompleteItems()
Dim textToMatch As String = Me.Text
Dim index As Integer = FindString(textToMatch)
If index <> CB_ERR Then
Me.SelectedIndex = index
Me.SelectionStart = textToMatch.Length
Me.SelectionLength = Me.Text.Length - Me.SelectionStart
End If
End Sub
Private Function FindString(textToMatch As String) As Integer
Dim findStruct As New NativeMethods.EDITBALLOONTIP()
findStruct.cbStruct = Marshal.SizeOf(findStruct)
findStruct.pszTitle = textToMatch
findStruct.pszText = String.Empty
findStruct.ttiIcon = NativeMethods.TTI_NONE
Dim findInfo As New NativeMethods.COMBOBOXINFO()
findInfo.cbSize = Marshal.SizeOf(findInfo)
If NativeMethods.GetComboBoxInfo(Me.Handle, findInfo) Then
Dim comboBoxHandle As IntPtr = findInfo.hwndCombo
Return CInt(SendMessage(comboBoxHandle, CB_FINDSTRING, IntPtr.Zero, Marshal.UnsafeAddrOfPinnedArrayElement(New Byte() {0, 0, 0, 0}, 0)))
End If
Return CB_ERR
End Function
End Class
Friend Class NativeMethods
Friend Const TTI_NONE As Integer = 0
Friend Const CBEM_INSERTITEM As Integer = &H14D
Friend Const CBEM_SETITEM As Integer = &H14E
Friend Const CBEM_GETITEM As Integer = &H14F
Friend Const CBEM_SETEXTENDEDSTYLE As Integer = &H156
Friend Const CBES_EX_NOEDITIMAGE As Integer = &H1
<StructLayout(LayoutKind.Sequential)>
Friend Structure EDITBALLOONTIP
Friend cbStruct As Integer
<MarshalAs(UnmanagedType.LPTStr)>
Friend pszTitle As String
<MarshalAs(UnmanagedType.LPTStr)>
Friend pszText As String
Friend ttiIcon As Integer
End Structure
<StructLayout(LayoutKind.Sequential)>
Friend Structure COMBOBOXINFO
Friend cbSize As Integer
Friend rcItem As RECT
Friend rcButton As RECT
Friend stateButton As Integer
Friend hwndCombo As IntPtr
Friend hwndEdit As IntPtr
Friend hwndList As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)>
Friend Structure RECT
Friend left As Integer
Friend top As Integer
Friend right As Integer
Friend bottom As Integer
End Structure
<DllImport("user32.dll")>
Friend Shared Function GetComboBoxInfo(hWnd As IntPtr, ByRef pcbi As COMBOBOXINFO) As Boolean
End Function
End Class