How do combo boxes, when displaying list items, intercept mouse events to anywhere on the form to hide the list?

前端 未结 2 498
清酒与你
清酒与你 2021-01-12 05:11

I\'m trying to implement a .net form control with functionality similar to a combo box, but I don\'t know the proper method to intercept mouse events anywhere on the form to

2条回答
  •  天涯浪人
    2021-01-12 05:41

    Just use a ToolStripControlHost along with the ToolStripDropDown, and it will work just like the ComboBox dropdown. You won't have to worry about handling the mouse events.

    I've used this before:

    Private Sub ShowControl(ByVal fromControl As Control, ByVal whichControl As Control)
      '\\ whichControl needs MinimumSize set:'
      whichControl.MinimumSize = whichControl.Size
    
      Dim toolDrop As New ToolStripDropDown()
      Dim toolHost As New ToolStripControlHost(whichControl)
      toolHost.Margin = New Padding(0)
      toolDrop.Padding = New Padding(0)
      toolDrop.Items.Add(toolHost)
      toolDrop.Show(Me, New Point(fromControl.Left, fromControl.Bottom))
    End Sub
    

    Quick Demo with a Button control on a form:

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
      ShowControl(Button1, New MonthCalendar)
    End Sub
    

    To answer the question in your title, I think the pinvoke calls of SetCapture and Release Capture are used to handle that type of functionality.

提交回复
热议问题