<pre>Public Class MySearchBoxNew Dim PopUpControl As ToolStripDropDown Dim List_Box As ListBox Dim Control_Host As ToolStripControlHost Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Down Then e.Handled = True e.SuppressKeyPress = True If Me.List_Box.Items.Count > 0 AndAlso Me.List_Box.SelectedIndex < Me.List_Box.Items.Count - 1 Then Me.List_Box.SelectedIndex = Me.List_Box.SelectedIndex + 1 End If ElseIf e.KeyCode = Keys.Up Then e.Handled = True e.SuppressKeyPress = True If Me.List_Box.Items.Count > 0 AndAlso Me.List_Box.SelectedIndex > 0 Then Me.List_Box.SelectedIndex = Me.List_Box.SelectedIndex - 1 End If ElseIf e.KeyCode = Keys.Escape Then e.Handled = True e.SuppressKeyPress = True Me.CloseDropDown() End If Call Me.OnKeyDown(e) End Sub Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged If Me.TextBox1.TextLength > 0 Then Me.ShowDropDown() Else Me.CloseDropDown() End If End Sub Private Sub MySearchBoxNew_Enter(sender As Object, e As System.EventArgs) Handles Me.Enter PopUpControl = New ToolStripDropDown List_Box = New ListBox Control_Host = New ToolStripControlHost(List_Box) List_Box.BorderStyle = BorderStyle.None List_Box.SelectionMode = SelectionMode.One List_Box.BindingContext = New BindingContext List_Box.IntegralHeight = True List_Box.Items.Clear() List_Box.Items.Add("A") List_Box.Items.Add("B") List_Box.Items.Add("c") List_Box.Items.Add("D") Control_Host.Padding = New Padding(0) Control_Host.Margin = New Padding(0) Control_Host.AutoSize = False PopUpControl.Padding = New Padding(0) PopUpControl.Margin = New Padding(0) PopUpControl.Width = Me.TextBox1.Width PopUpControl.AutoSize = True PopUpControl.AutoClose = False PopUpControl.Items.Add(Control_Host) End Sub Private Sub MySearchBoxNew_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave Me.CloseDropDown() End Sub Private Sub CloseDropDown() PopUpControl.Close() Control_Host = Nothing PopUpControl = Nothing List_Box = Nothing End Sub Private Sub ShowDropDown() Dim pnt As Point = New Point(Me.TextBox1.Location.X, Me.TextBox1.Location.Y + Me.TextBox1.Height) Dim PointToShowMenu As Point = Me.PointToScreen(pnt) If PopUpControl Is Nothing Then PopUpControl = New ToolStripDropDown If List_Box Is Nothing Then List_Box = New ListBox If Control_Host Is Nothing Then Control_Host = New ToolStripControlHost(List_Box) PopUpControl.Show(PointToShowMenu) End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)