8

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 un-expand the list of items.

How do I prevent other controls from responding to mouse events while the list is being shown?

How do I efficiently and safely catch a mouse click event to anywhere on the form, to hide the expanded list?

Craig Gidney
  • 17,763
  • 5
  • 68
  • 136

2 Answers2

9

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.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
1

Control.Capture

As explained in the documentation, you now "Own" the mouse (until someone else captures it - though that's bad form). You receive all mouse messages and can handle a "Down" not on your control to dismiss.

John Arlen
  • 6,539
  • 2
  • 33
  • 42
  • This only seems to half work. All of the controls within the control with mouse capture stop working. For example, the scroll bar within a list box with mouse capture doesn't work until capture is released. – Craig Gidney Dec 23 '11 at 20:09