不过我也最近才经常接触的winform。记录之
我希望能够在CheckedListBox中实现如下的效果:


下面是我改造的过程
首先查了一下msdn,
http://msdn2.microsoft.com/zh-cn/library/system.windows.forms.checkedlistbox_members(VS.80).aspx
会看到如下几个属性,
![]() |
DataSource | 获取或设置控件的数据源。此属性与此类无关。 |
![]() |
DisplayMember | 此属性与此类无关。 |
![]() |
ValueMember | 获取或设置一个字符串,该字符串指定要从中取值的数据源的属性此属性与此类无关。 |
使用Reflector查看了一CheckedListBox的关系

在CheckedListBox中本身已经实现了这三个属性,仅是ms使用了如下的特性,使我们不能用它了,[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
知道了来龙去脉就好改造了,在原CheckedListBox基础上再扩展一个类ExCheckedListBox
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.ComponentModel;
5
using System.Drawing;
6
using System.Windows.Forms;
7
using System.Drawing.Design;
8
9
namespace CustomControls
10
{
11
/// <summary>
12
/// (eraghi)
13
/// Extended CheckedListBox with binding facilities (Value property)
14
/// </summary>
15
[ToolboxBitmap(typeof(CheckedListBox))]
16
public class ExCheckedListBox : CheckedListBox
17
{
18
/// <summary>
19
/// Default constructor
20
/// </summary>
21
public ExCheckedListBox()
22
{
23
this.CheckOnClick = true;
24
25
}
26
27
28
29
/// <summary>
30
/// Gets or sets the property to display for this CustomControls.CheckedListBox.
31
///
32
/// Returns:
33
/// A System.String specifying the name of an object property that is contained
34
/// in the collection specified by the CustomControls.CheckedListBox.DataSource
35
/// property. The default is an empty string ("").
36
/// </summary>
37
[DefaultValue("")]
38
[TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
39
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
40
[Browsable(true)]
41
public new string DisplayMember
42
{
43
get
44
{
45
return base.DisplayMember;
46
}
47
set
48
{
49
base.DisplayMember = value;
50
51
}
52
}
53
54
/// <summary>
55
/// Gets or sets the data source for this CustomControls.CheckedListBox.
56
/// Returns:
57
/// An object that implements the System.Collections.IList or System.ComponentModel.IListSource
58
/// interfaces, such as a System.Data.DataSet or an System.Array. The default
59
/// is null.
60
///
61
///Exceptions:
62
/// System.ArgumentException:
63
/// The assigned value does not implement the System.Collections.IList or System.ComponentModel.IListSource
64
/// interfaces.
65
/// </summary>
66
[DefaultValue("")]
67
[AttributeProvider(typeof(IListSource))]
68
[RefreshProperties(RefreshProperties.All)]
69
[Browsable(true)]
70
public new object DataSource {
71
get
72
{
73
return base.DataSource;
74
}
75
set
76
{
77
base.DataSource = value;
78
79
}
80
}
81
private int value;
82
[DefaultValue(""), TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
83
[Browsable(true)]
84
public new string ValueMember
85
{
86
get
87
{
88
///Gets checked items in decimal mode from binary mode
89
90
try
91
{
92
//each item in list has a number that is binary number in decimal mode
93
//this number represents that number
94
int poweredNumber = 1;
95
//loop in all items of list
96
for (int i = 0; i < this.Items.Count; i++)
97
{
98
//if item checked and the value doesn't contains poweredNumber then
99
//add poweredNumber to the value
100
if ((this.GetItemChecked(i)))
101
this.value |= poweredNumber;
102
//else if poweredNumber exists in the value remove from it
103
else if ((this.value & poweredNumber) != 0)
104
this.value -= poweredNumber;
105
106
//raise to the power
107
poweredNumber *= 2;
108
}
109
}
110
catch (ArgumentException ex)
111
{
112
throw ex;
113
}
114
catch (Exception ex)
115
{
116
throw ex;
117
}
118
119
120
return base.ValueMember;
121
}
122
set
123
{
124
base.ValueMember = value;
125
if (base.ValueMember.ToLower() == "false")
126
this.value = 0;
127
else
128
this.value = 1;
129
130
try
131
{
132
//each item in list has a number that is binary number in decimal mode
133
//this number represents that number
134
int poweredNumber = 1;
135
//loop in all items of list
136
for (int i = 0; i < this.Items.Count; i++)
137
{
138
//if poweredNumber exists in the value set checked on item
139
if ((this.value & poweredNumber) != 0)
140
this.SetItemCheckState(i, CheckState.Checked);
141
//else remove checked from item
142
else
143
this.SetItemCheckState(i, CheckState.Unchecked);
144
145
//raise to the power
146
poweredNumber *= 2;
147
}
148
}
149
catch (ArgumentException ex)
150
{
151
throw ex;
152
}
153
catch (Exception ex)
154
{
155
throw ex;
156
}
157
}
158
}
159
}
160
}
161
其中控件的主要代码,这位伊郞大哥的code,link如下:
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

http://www.codeproject.com/cs/combobox/ExCheckedListBox.asp