Click here to Skip to main content
15,887,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 so 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 or if we shift to another window.
It should be closed immediately, just like what happens with combobox dropdown

Here is my code..
VB
<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



What I have tried:

I have followed This From Codeproject
Posted
Updated 2-May-19 2:44am
Comments
Richard MacCutchan 27-Apr-19 4:27am    
You should post your question in the form at the end of the article, so the author can help you.
[no name] 27-Apr-19 8:10am    
Closed "when"? How does it get "opened" in the first place? When does moving and sizing come into it? No offense to the author, but that article is 10 years old. I suspect the "list box" is "smarter" these days. You got a lot of stuff going on that seems like "make work".

You're asking the wrong questions.

1 solution

I've never had luck getting drop-downs from ToolStrips to work the way I wanted. I've always ended up just using controls that I position myself, relative to the other controls they interact with, and then show/hide/BringToFront the controls appropriately.

For example, you could add a listbox to your control collection and hide it. When it's time to display it, set its Location relative to the textbox (listbox top/left relative to the textbox's bottom/left), show it and call BringToFront to have it appear above other controls.

If you want the listbox to go away when the form moves, resizes, etc just set Visible = False in the appropriate event handlers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900