Hi..
I need to create an usercontrol containing an TextBox, which searches for the values in the DB and displays in an listbox so that the user can select appropriate value.

To do after googling ,i am using an ToolStripControlHost to display the listbox.
The problem i am facing is the ToolStripDropDown keeps displayed when the form is resized or moved. It should be closed immediately, just like what happens with combobox dropdown

Here is my code..
vb Code:
  1. Public Class MySearchBoxNew
  2.  
  3.     Dim PopUpControl As ToolStripDropDown
  4.     Dim List_Box As ListBox
  5.     Dim Control_Host As ToolStripControlHost
  6.  
  7.  
  8.     Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
  9.         If e.KeyCode = Keys.Down Then
  10.             e.Handled = True
  11.             e.SuppressKeyPress = True
  12.             If Me.List_Box.Items.Count > 0 AndAlso Me.List_Box.SelectedIndex < Me.List_Box.Items.Count - 1 Then
  13.                 Me.List_Box.SelectedIndex = Me.List_Box.SelectedIndex + 1
  14.             End If
  15.         ElseIf e.KeyCode = Keys.Up Then
  16.             e.Handled = True
  17.             e.SuppressKeyPress = True
  18.             If Me.List_Box.Items.Count > 0 AndAlso Me.List_Box.SelectedIndex > 0 Then
  19.                 Me.List_Box.SelectedIndex = Me.List_Box.SelectedIndex - 1
  20.             End If
  21.         ElseIf e.KeyCode = Keys.Escape Then
  22.             e.Handled = True
  23.             e.SuppressKeyPress = True
  24.             Me.CloseDropDown()
  25.         End If
  26.         Call Me.OnKeyDown(e)
  27.     End Sub
  28.  
  29.     Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
  30.         If Me.TextBox1.TextLength > 0 Then
  31.             Me.ShowDropDown()
  32.         Else
  33.             Me.CloseDropDown()
  34.         End If
  35.  
  36.     End Sub
  37.  
  38.     Private Sub MySearchBoxNew_Enter(sender As Object, e As System.EventArgs) Handles Me.Enter
  39.         PopUpControl = New ToolStripDropDown
  40.         List_Box = New ListBox
  41.         Control_Host = New ToolStripControlHost(List_Box)
  42.  
  43.  
  44.         List_Box.BorderStyle = BorderStyle.None
  45.         List_Box.SelectionMode = SelectionMode.One
  46.         List_Box.BindingContext = New BindingContext
  47.         List_Box.IntegralHeight = True
  48.  
  49.         List_Box.Items.Clear()
  50.         List_Box.Items.Add("A")
  51.         List_Box.Items.Add("B")
  52.         List_Box.Items.Add("c")
  53.         List_Box.Items.Add("D")
  54.  
  55.         Control_Host.Padding = New Padding(0)
  56.         Control_Host.Margin = New Padding(0)
  57.         Control_Host.AutoSize = False
  58.  
  59.  
  60.         PopUpControl.Padding = New Padding(0)
  61.         PopUpControl.Margin = New Padding(0)
  62.         PopUpControl.Width = Me.TextBox1.Width
  63.         PopUpControl.AutoSize = True
  64.         PopUpControl.AutoClose = False
  65.         PopUpControl.Items.Add(Control_Host)
  66.  
  67.     End Sub
  68.  
  69.     Private Sub MySearchBoxNew_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave
  70.         Me.CloseDropDown()
  71.     End Sub
  72.  
  73.    
  74.     Private Sub CloseDropDown()
  75.         PopUpControl.Close()
  76.         Control_Host = Nothing
  77.         PopUpControl = Nothing
  78.         List_Box = Nothing
  79.     End Sub
  80.     Private Sub ShowDropDown()
  81.         Dim pnt As Point = New Point(Me.TextBox1.Location.X, Me.TextBox1.Location.Y + Me.TextBox1.Height)
  82.         Dim PointToShowMenu As Point = Me.PointToScreen(pnt)
  83.  
  84.         If PopUpControl Is Nothing Then PopUpControl = New ToolStripDropDown
  85.         If List_Box Is Nothing Then List_Box = New ListBox
  86.         If Control_Host Is Nothing Then Control_Host = New ToolStripControlHost(List_Box)
  87.  
  88.         PopUpControl.Show(PointToShowMenu)
  89.     End Sub
  90. End Class