-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchingComboBox.cs
203 lines (174 loc) · 6.68 KB
/
SearchingComboBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RealBookExtractor {
/// <summary>
/// This is a combo box that allows the user to type into the edit box,
/// but searches for the first item matching what they have typed in, and
/// prevents them entering anything not in the list.
/// </summary>
public class SearchingComboBox : System.Windows.Forms.ComboBox {
private bool _allowNoSelection = true;
private bool _acceptTabKey = false;
private bool _readOnly = false;
private object _savedItem;
private bool _useSavedItem;
public SearchingComboBox() {
base.DropDownStyle = ComboBoxStyle.DropDown;
base.Text = null;
SetStyle(ControlStyles.EnableNotifyMessage, true);
InputMustMatch = true;
}
// Prevent it appearing in the properties window.
[Browsable(false)]
public new ComboBoxStyle DropDownStyle {
get { return ComboBoxStyle.DropDown; }
}
protected override void OnDropDownStyleChanged(EventArgs e) {
base.DropDownStyle = ComboBoxStyle.DropDown;
}
protected override void OnKeyDown(KeyEventArgs e) {
if (_readOnly) {
e.Handled = true;
} else if (e.KeyCode == Keys.Delete) {
if (Text.Length == SelectionLength && AllowNoSelection) {
Text = String.Empty;
base.SelectedItem = null;
}
e.Handled = true;
}
base.OnKeyDown(e);
}
/// <summary>
/// If true (the default) deleting the text in the edit box causes no item (null)
/// to be selected, otherwise it retains any current selection.
/// </summary>
[Browsable(true),
Category("Behavior"),
Description("Controls whether deleting the text causes nothing to be selected"),
DefaultValue(true)]
public bool AllowNoSelection {
get { return _allowNoSelection; }
set { _allowNoSelection = value; }
}
/// <summary>
/// If true, the control processes the Tab key in its own KeyDown/KeyUp events.
/// </summary>
[Browsable(true),
Category("Behaviour"),
Description("Whether the control accepts the Tab key (processes it itself)"),
DefaultValue(false)]
public bool AcceptTabKey {
get { return _acceptTabKey; }
set { _acceptTabKey = value; }
}
/// <summary>
/// If true, the user cannot type into the edit box, or select from the drop down.
/// </summary>
[Browsable(true),
Category("Behavior"),
Description("Whether the control allows any user selection"),
DefaultValue(false)]
public bool ReadOnly {
get { return _readOnly; }
set { _readOnly = value; }
}
[Browsable(true),
Category("Behavior"),
Description("Whether any input has to match an item in the list"),
DefaultValue(true)]
public bool InputMustMatch { get; set; }
protected override bool IsInputKey(Keys keyData) {
if (keyData == Keys.Tab) return _acceptTabKey;
return base.IsInputKey(keyData);
}
protected override void OnKeyPress(KeyPressEventArgs e) {
base.OnKeyPress(e);
if (e.Handled) return;
/* Whatever happens, we have handled the key, and
* and don't want the base class to do anything with it */
e.Handled = true;
if (_readOnly) return;
// Find the text entered so far - everything that is not selected.
string soFar = Text.Substring(0, SelectionStart);
// Now modify that with the key entered.
if (e.KeyChar == '\b')
if (soFar.Length < 2)
soFar = String.Empty;
else
soFar = soFar.Substring(0, soFar.Length - 1);
else
soFar += e.KeyChar;
if (soFar.Length == 0 && !AllowNoSelection && base.SelectedItem != null) {
Text = Items[0].ToString();
SelectionStart = 0;
SelectionLength = Text.Length;
return;
}
if (soFar.Length == 0) {
Text = String.Empty;
base.SelectedItem = null;
return;
}
int pos = this.FindString(soFar);
// If it's not in the list, ignore the key press
if (InputMustMatch && pos < 0) return;
// Note that the SelectedIndexChanged event gets raised by this automatically.
if (!InputMustMatch && pos < 0) {
Text = soFar;
} else if (pos >= 0) {
Text = Items[pos].ToString();
}
SelectionStart = soFar.Length;
SelectionLength = Math.Max(Text.Length - soFar.Length, 0);
}
protected override void OnKeyUp(KeyEventArgs e) {
if (_readOnly)
e.Handled = true;
base.OnKeyUp(e);
}
public override string Text {
get {
return base.Text;
}
set {
// Make sure it can be set only to valid values
if ((value == null || value.Length == 0) && !AllowNoSelection)
return;
if (value != null && value.Length > 0) {
int pos = FindString(value);
if (InputMustMatch && (FindString(value) < 0 || Items[pos].ToString() != value))
return;
}
base.Text = value;
SelectionStart = Text.Length;
}
}
protected override void OnDropDown(EventArgs e) {
base.OnDropDown(e);
_savedItem = SelectedItem;
}
protected override void OnSelectedIndexChanged(EventArgs e) {
if (_readOnly && _useSavedItem) {
if (SelectedItem != _savedItem)
SelectedItem = _savedItem;
} else
// This shouldn't raise any events.
base.OnSelectedIndexChanged(e);
}
public new object SelectedItem {
get { return base.SelectedItem; }
set {
try {
_useSavedItem = false;
base.SelectedItem = value;
} finally {
_useSavedItem = true;
}
}
}
}
}