7

I am trying to model the behavior of a ComboBox dropdown (or other drop downs for that matter, including context menus) where the drop down closes when you click anywhere else, even on something that can't be focused.

I've tried subscribing for events such as MouseCaptureChanged, LostFocus, and Leave. I have a custom UserControl which is acting as a dropdown and I just want to close it up when the user clicks anywhere else.

This seems like something that's done in many controls so I'd be surprised if there wasn't a simple way to do it.

So far the overcomplicated methods I can come up with to do this are using pinvoke and the SetCapture() function, or to create a MessageFilter. If these are the only options I am not sure which is better.

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
  • 3
    An easy way to implement a DropDown like control is to use a ToolStripControlHost and a ToolStripDropDown. Works just like a ComboBox. See [How do combo boxes, when displaying list items, intercept mouse events to anywhere on the form to hide the list?](http://stackoverflow.com/a/8611382/719186) – LarsTech Feb 14 '12 at 18:45
  • I am trying the ToolStripControlHost. I can't get rid of an ugly border along the top of the popup, or the flickering of the ComboBox whenever you open the dropdown. – Trevor Elliott Feb 14 '12 at 18:56
  • 3
    Make sure to set the Margins of the ToolStripContorlHost and the Padding of the ToolStripDropDown to 0. Flickering on your control is a different issue — are you trying to use a ComboBox with a custom drop down? – LarsTech Feb 14 '12 at 19:06
  • Yes. I'm overriding OnDropDown and I'm resetting the IntegralHeight property in order to force it to close. It makes it so it doesn't appear but still flickers. – Trevor Elliott Feb 14 '12 at 19:13
  • @Moozhe, are you using Windows Forms or WPF? – Dima Apr 07 '12 at 18:11
  • Try doublebuffering your control and see if it still flickers? – Satyajit May 07 '12 at 00:05

2 Answers2

1

The ComboBox is constructed from 2 controls.

  • Base - visible when not active (Control)
  • DropDownList - visible during edit mode or list selection mode (Window or Form)

Normally the Base is visible. When the user clicks onto the ComboBox, the Base control hides and the DropDownList control shows up. This switch is done on the background, so for the user it seems the control just expanded.

The event you want to catch is done through the DropDownList Window. If you click somewhere onto your client area, the DropDownList Window receives the WM_KILLFOCUS event through it's WndProc(Message% m) method. Then sends to the parent window (the Base control) a WM_COMMAND (OCM_COMMAND) message with WParam=526318 (HIWORD(WParam)=8) and the Base control knows he should hide the DropDownList Window.

So, what you need to do is implement the additional DropDown Window and catch the WM_KILLFOCUS event.

Zoltan Tirinda
  • 769
  • 8
  • 25
0

The templates of the controls you've mentioned are using for the dropdown lists a popup as a container with the StaysOpen property set on false (which is the default i think).

ComboBox template example

Avram Tudor
  • 1,468
  • 12
  • 18