在窗体中加入一个 Listbox、Combox、Textbox 控件。
在Textbox中逐步输入字符,会快速地选择并定位最接近的项。
Public Class Form1
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.AsAny)> ByVal lParam As Object) As Int32
'Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Message) As Int32
Const WM_SETTEXT = &HC
Const CB_ADDSTRING = &H143
Const LB_FINDSTRING = &H18F
Const CB_FINDSTRING = &H14C
Const LB_SETHORIZONTALEXTENT = &H194
Const CB_SETHORIZONTALEXTENT = &H15E
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("a")
ListBox1.Items.Add("ab")
ListBox1.Items.Add("ac")
ListBox1.Items.Add("abd")
ListBox1.Items.Add("abc")
ListBox1.Items.Add("bc")
ListBox1.Items.Add("bs")
ListBox1.Items.Add("bcd")
For Each s As String In ListBox1.Items
ComboBox1.Items.Add(s)
Next
SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, 250, 0)
SendMessage(ComboBox1.Handle, CB_SETHORIZONTALEXTENT, 250, 0)
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ListBox1.SelectedIndex = SendMessage(ListBox1.Handle, LB_FINDSTRING, -1, TextBox1.Text)
ComboBox1.SelectedIndex = SendMessage(ComboBox1.Handle, CB_FINDSTRING, -1, TextBox1.Text)
End Sub
End Class