Skip to content

Commit 900afc7

Browse files
committed
[ComboBox] Close dropdown on mouse up instead of mouse down.
1 parent 6af7747 commit 900afc7

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/Modern.Forms/ComboBox.cs

+10-8
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public class ComboBox : Control
1111
{
1212
private PopupWindow? popup;
1313
private readonly ListBox popup_listbox;
14-
private bool suppress_popup_close;
1514

1615
/// <summary>
1716
/// Initializes a new instance of the ComboBox class.
1817
/// </summary>
1918
public ComboBox ()
2019
{
2120
popup_listbox = new ListBox { Dock = DockStyle.Fill, ShowHover = true };
21+
popup_listbox.MouseUp += ListBox_OnMouseUp;
2222
popup_listbox.SelectedIndexChanged += ListBox_SelectedIndexChanged;
2323
}
2424

@@ -48,6 +48,8 @@ protected override void Dispose (bool disposing)
4848
popup?.Close ();
4949
popup = null;
5050

51+
popup_listbox.MouseUp -= ListBox_OnMouseUp;
52+
popup_listbox.SelectedIndexChanged -= ListBox_SelectedIndexChanged;
5153
popup_listbox.Dispose ();
5254
}
5355

@@ -92,13 +94,17 @@ public bool DroppedDown {
9294
/// </summary>
9395
public ListBoxItemCollection Items => popup_listbox.Items;
9496

97+
// We need to close the dropdown on mouse up instead of mouse down so
98+
// that the mouse click/up events don't leak to the control underneath the dropdown.
99+
private void ListBox_OnMouseUp (object? sender, MouseEventArgs e)
100+
{
101+
DroppedDown = false;
102+
}
103+
95104
// When the selected item of the popup ListBox changes, update the ComboBox
96105
private void ListBox_SelectedIndexChanged (object? sender, EventArgs e)
97106
{
98107
if (popup_listbox.SelectedIndex > -1) {
99-
if (!suppress_popup_close)
100-
DroppedDown = false;
101-
102108
Invalidate ();
103109

104110
OnSelectedIndexChanged (e);
@@ -148,11 +154,7 @@ protected override void OnKeyUp (KeyEventArgs e)
148154
return;
149155
}
150156

151-
// If you mouse click an item we automatically close the dropdown,
152-
// we don't want that behavior when using the keyboard.
153-
suppress_popup_close = true;
154157
popup_listbox.RaiseKeyUp (e);
155-
suppress_popup_close = false;
156158

157159
if (e.Handled)
158160
return;

0 commit comments

Comments
 (0)