-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomComboBox.cs
331 lines (296 loc) · 11.7 KB
/
CustomComboBox.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace HiraKata_Kaizen {
[DefaultEvent("OnSelectedIndexChanged")]
public class CustomComboBox : UserControl {
//Fields
Color backColor = Color.FromArgb(30, 30, 34);
Color foreColor = Color.White;
Color iconColor = Color.Maroon;
Color listBackColor = Color.Black;
Color listForeColor = Color.White;
Color borderColor = Color.Maroon;
byte borderSize = 1;
//Items
ComboBox comboBox;
Label label;
Button btnIcon;
//Events
public event EventHandler OnSelectedIndexChanged;//Default event
//Constructor
public CustomComboBox() {
comboBox = new ComboBox();
label = new Label();
btnIcon = new Button();
SuspendLayout();
//ComboBox: Dropdown list
comboBox.BackColor = listBackColor;
comboBox.Font = new Font(Font.Name, 10F);
comboBox.ForeColor = foreColor;
comboBox.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);//Default event
comboBox.TextChanged += new EventHandler(ComboBox_TextChanged);//Refresh text
comboBox.Click += new EventHandler(ComboBox_Click);
//Button: Icon
btnIcon.Dock = DockStyle.Right;
btnIcon.FlatStyle = FlatStyle.Flat;
btnIcon.FlatAppearance.BorderSize = 0;
btnIcon.BackColor = backColor;
btnIcon.Size = new Size(30, 30);
btnIcon.FlatAppearance.MouseOverBackColor = Color.FromArgb(100, Color.Red);
//btnIcon.Cursor = Cursors.Hand;
btnIcon.Click += new EventHandler(Icon_Click);//Open dropdown list
btnIcon.Paint += new PaintEventHandler(Icon_Paint);//Draw icon
//Label: Text
label.Dock = DockStyle.Fill;
label.AutoSize = false;
label.BackColor = backColor;
label.ForeColor = foreColor;
label.TextAlign = ContentAlignment.MiddleLeft;
label.Padding = new Padding(8, 0, 0, 0);
label.Font = new Font(Font.Name, 10F);
//->Attach label events to user control event
label.Click += new EventHandler(Surface_Click);//Select combo box
label.MouseEnter += new EventHandler(Surface_MouseEnter);
label.MouseLeave += new EventHandler(Surface_MouseLeave);
//User Control
Controls.Add(label);//2
Controls.Add(btnIcon);//1
Controls.Add(comboBox);//0
MinimumSize = new Size(121, 29);
Size = new Size(121, 29);
Padding = new Padding(borderSize);//Border Size
Font = new Font(this.Font.Name, 10F);
base.BackColor = borderColor; //Border Color
ResumeLayout();
AdjustComboBoxDimensions();
}
// Private methods
void ComboBox_Click(object sender, EventArgs e) {
//Open dropdown list
comboBox.Select();
comboBox.DroppedDown = true;
}
void AdjustComboBoxDimensions() {
comboBox.Width = Width - Padding.Left - Padding.Right;
comboBox.Height = label.Height;
comboBox.Location = new Point() {
X = Padding.Left,
Y = label.Bottom - comboBox.Height
};
}
//Event methods
//-> Default event
void ComboBox_SelectedIndexChanged(object sender, EventArgs e) {
if (OnSelectedIndexChanged != null)
OnSelectedIndexChanged.Invoke(sender, e);
//Refresh text
label.Text = comboBox.Text;
}
//-> Items actions
void Icon_Click(object sender, EventArgs e) {
//Open dropdown list
comboBox.Select();
comboBox.DroppedDown = true;
}
void Surface_Click(object sender, EventArgs e) {
//Attach label click to user control click
OnClick(e);
//Select combo box
comboBox.Select();
if (comboBox.DropDownStyle == ComboBoxStyle.DropDownList)
comboBox.DroppedDown = true;//Open dropdown list
}
void ComboBox_TextChanged(object sender, EventArgs e) {
//Refresh text
label.Text = comboBox.Text;
}
//-> Draw icon
private void Icon_Paint(object sender, PaintEventArgs e) {
//Fields
byte iconWidht = 14;
byte iconHeight = 6;
var rectIcon = new Rectangle((btnIcon.Width - iconWidht) / 2, (btnIcon.Height - iconHeight) / 2, iconWidht, iconHeight);
Graphics graph = e.Graphics;
//Draw arrow down icon
using (GraphicsPath path = new GraphicsPath())
using (Pen pen = new Pen(iconColor, 2)) {
graph.SmoothingMode = SmoothingMode.AntiAlias;
path.AddLine(rectIcon.X, rectIcon.Y, rectIcon.X + (iconWidht / 2), rectIcon.Bottom);
path.AddLine(rectIcon.X + (iconWidht / 2), rectIcon.Bottom, rectIcon.Right, rectIcon.Y);
graph.DrawPath(pen, path);
}
}
//Properties
//-> Appearance
[Category("Appearance")]
public new Color BackColor {
get { return backColor; }
set {
backColor = value;
label.BackColor = backColor;
btnIcon.BackColor = backColor;
}
}
[Category("Appearance")]
public Color IconColor {
get { return iconColor; }
set {
iconColor = value;
btnIcon.Invalidate();//Redraw icon
}
}
[Category("Appearance")]
public Color ListBackColor {
get { return listBackColor; }
set {
listBackColor = value;
comboBox.BackColor = listBackColor;
}
}
[Category("Appearance")]
public Color ListTextColor {
get { return listForeColor; }
set {
listForeColor = value;
comboBox.ForeColor = listForeColor;
}
}
[Category("Appearance")]
public Color BorderColor {
get { return borderColor; }
set {
borderColor = value;
base.BackColor = borderColor; //Border Color
}
}
[Category("Appearance")]
public int BorderSize {
get { return borderSize; }
set {
borderSize = (byte)value;
Padding = new Padding(borderSize);//Border Size
AdjustComboBoxDimensions();
}
}
[Category("Appearance")]
public override Color ForeColor {
get { return base.ForeColor; }
set {
base.ForeColor = value;
label.ForeColor = value;
}
}
[Category("Appearance")]
public override Font Font {
get { return base.Font; }
set {
base.Font = value;
label.Font = value;
comboBox.Font = value;//Optional
}
}
[Category("Appearance")]
public string Texts {
get { return label.Text; }
set { label.Text = value; }
}
[Category("Appearance")]
public ComboBoxStyle DropDownStyle {
get { return comboBox.DropDownStyle; }
set {
if (comboBox.DropDownStyle != ComboBoxStyle.Simple)
comboBox.DropDownStyle = value;
}
}
//Properties
//-> Data
[Category("Data")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[Localizable(true)]
[MergableProperty(false)]
public ComboBox.ObjectCollection Items {
get { return comboBox.Items; }
}
[Category("Data")]
[AttributeProvider(typeof(IListSource))]
[DefaultValue(null)]
public object DataSource {
get { return comboBox.DataSource; }
set { comboBox.DataSource = value; }
}
[Category("Data")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[EditorBrowsable(EditorBrowsableState.Always)]
[Localizable(true)]
public AutoCompleteStringCollection AutoCompleteCustomSource {
get { return comboBox.AutoCompleteCustomSource; }
set { comboBox.AutoCompleteCustomSource = value; }
}
[Category("Data")]
[Browsable(true)]
[DefaultValue(AutoCompleteSource.None)]
[EditorBrowsable(EditorBrowsableState.Always)]
public AutoCompleteSource AutoCompleteSource {
get { return comboBox.AutoCompleteSource; }
set { comboBox.AutoCompleteSource = value; }
}
[Category("Data")]
[Browsable(true)]
[DefaultValue(AutoCompleteMode.None)]
[EditorBrowsable(EditorBrowsableState.Always)]
public AutoCompleteMode AutoCompleteMode {
get { return comboBox.AutoCompleteMode; }
set { comboBox.AutoCompleteMode = value; }
}
[Category("Data")]
[Bindable(true)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public object SelectedItem {
get { return comboBox.SelectedItem; }
set { comboBox.SelectedItem = value; }
}
[Category("Data")]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int SelectedIndex {
get { return comboBox.SelectedIndex; }
set { comboBox.SelectedIndex = value; }
}
[Category("Data")]
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
[TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember {
get { return comboBox.DisplayMember; }
set { comboBox.DisplayMember = value; }
}
[Category("Data")]
[DefaultValue("")]
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string ValueMember {
get { return comboBox.ValueMember; }
set { comboBox.ValueMember = value; }
}
//->Attach label events to user control event
private void Surface_MouseLeave(object sender, EventArgs e) {
OnMouseLeave(e);
}
private void Surface_MouseEnter(object sender, EventArgs e) {
OnMouseEnter(e);
}
//::::+
//Overridden methods
protected override void OnResize(EventArgs e) {
base.OnResize(e);
AdjustComboBoxDimensions();
}
}
}