在mouseclick上关闭toolstripdropdown [英] Close toolstripdropdown on mouseclick

查看:117
本文介绍了在mouseclick上关闭toolstripdropdown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

我需要创建一个包含TextBox的usercontrol,它会搜索数据库中的值并显示在列表框中,以便用户可以选择合适的值。



为了在谷歌搜索之后这样做,我使用ToolStripControlHost来显示列表框。

我面临的问题是当表单是时,ToolStripDropDown会一直显示调整大小或移动或如果我们转移到另一个窗口。

它应该立即关闭,就像组合框下拉时一样



这是我的代码..

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..

<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







我的尝试:



我跟着这来自Codeproject

推荐答案

我从来没有幸运从ToolStrips下载到我想要的工作方式。我总是只使用我自己定位的控件,相对于他们与之交互的其他控件,然后适当地显示/隐藏/ BringToFront控件。



例如,您可以将一个列表框添加到控件集合中并将其隐藏。当显示它时,相对于文本框设置其位置(列表框相对于文本框的左下角/左侧),显示它并调用BringToFront使其显示在其他控件上方。



如果你希望列表框在表单移动,调整大小等时消失,只需在相应的事件处理程序中设置Visible = False。
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.


这篇关于在mouseclick上关闭toolstripdropdown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆