@@ -11,14 +11,14 @@ public class ComboBox : Control
11
11
{
12
12
private PopupWindow ? popup ;
13
13
private readonly ListBox popup_listbox ;
14
- private bool suppress_popup_close ;
15
14
16
15
/// <summary>
17
16
/// Initializes a new instance of the ComboBox class.
18
17
/// </summary>
19
18
public ComboBox ( )
20
19
{
21
20
popup_listbox = new ListBox { Dock = DockStyle . Fill , ShowHover = true } ;
21
+ popup_listbox . MouseUp += ListBox_OnMouseUp ;
22
22
popup_listbox . SelectedIndexChanged += ListBox_SelectedIndexChanged ;
23
23
}
24
24
@@ -48,6 +48,8 @@ protected override void Dispose (bool disposing)
48
48
popup ? . Close ( ) ;
49
49
popup = null ;
50
50
51
+ popup_listbox . MouseUp -= ListBox_OnMouseUp ;
52
+ popup_listbox . SelectedIndexChanged -= ListBox_SelectedIndexChanged ;
51
53
popup_listbox . Dispose ( ) ;
52
54
}
53
55
@@ -92,13 +94,17 @@ public bool DroppedDown {
92
94
/// </summary>
93
95
public ListBoxItemCollection Items => popup_listbox . Items ;
94
96
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
+
95
104
// When the selected item of the popup ListBox changes, update the ComboBox
96
105
private void ListBox_SelectedIndexChanged ( object ? sender , EventArgs e )
97
106
{
98
107
if ( popup_listbox . SelectedIndex > - 1 ) {
99
- if ( ! suppress_popup_close )
100
- DroppedDown = false ;
101
-
102
108
Invalidate ( ) ;
103
109
104
110
OnSelectedIndexChanged ( e ) ;
@@ -148,11 +154,7 @@ protected override void OnKeyUp (KeyEventArgs e)
148
154
return ;
149
155
}
150
156
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 ;
154
157
popup_listbox . RaiseKeyUp ( e ) ;
155
- suppress_popup_close = false ;
156
158
157
159
if ( e . Handled )
158
160
return ;
0 commit comments