-
Notifications
You must be signed in to change notification settings - Fork 4
/
ComboBoxEx.cs
82 lines (69 loc) · 1.78 KB
/
ComboBoxEx.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
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
namespace TSDev {
class ComboBoxEx : ComboBox {
private ImageList imageList;
public ImageList ImageList {
get { return imageList; }
set { imageList = value; }
}
public ComboBoxEx() {
DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs ea) {
ea.DrawBackground();
ea.DrawFocusRectangle();
ComboBoxExItem item;
Size imageSize = imageList.ImageSize;
Rectangle bounds = ea.Bounds;
try {
item = (ComboBoxExItem)Items[ea.Index];
if (item.ImageIndex != -1) {
imageList.Draw(ea.Graphics, bounds.Left, bounds.Top,
item.ImageIndex);
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left + imageSize.Width, bounds.Top);
} else {
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
} catch {
if (ea.Index != -1) {
ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
} else {
ea.Graphics.DrawString(Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
base.OnDrawItem(ea);
}
}
class ComboBoxExItem {
private string _text;
public string Text {
get { return _text; }
set { _text = value; }
}
private int _imageIndex;
public int ImageIndex {
get { return _imageIndex; }
set { _imageIndex = value; }
}
public ComboBoxExItem()
: this("") {
}
public ComboBoxExItem(string text)
: this(text, -1) {
}
public ComboBoxExItem(string text, int imageIndex) {
_text = text;
_imageIndex = imageIndex;
}
public override string ToString() {
return _text;
}
}
}