这个话题似乎很早就有说了,
不过我也最近才经常接触的winform。记录之
我希望能够在CheckedListBox中实现如下的效果:
 通过它,可以将CheckedListBox的复选框绑定到一个数据库的boolean型的字段上,效果如下
通过它,可以将CheckedListBox的复选框绑定到一个数据库的boolean型的字段上,效果如下

下面是我改造的过程
首先查了一下msdn,
http://msdn2.microsoft.com/zh-cn/library/system.windows.forms.checkedlistbox_members(VS.80).aspx
会看到如下几个属性,
    
        
    
        
    
        
但是这三个属性是我们需要的,
使用Reflector查看了一CheckedListBox的关系
在CheckedListBox中本身已经实现了这三个属性,仅是ms使用了如下的特性,使我们不能用它了,[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
知道了来龙去脉就好改造了,在原CheckedListBox基础上再扩展一个类ExCheckedListBox
http://www.codeproject.com/cs/combobox/ExCheckedListBox.asp
 
不过我也最近才经常接触的winform。记录之
我希望能够在CheckedListBox中实现如下的效果:


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

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

9
 namespace CustomControls
namespace CustomControls10
 {
{11
 /// <summary>
    /// <summary>12
 /// (eraghi)
    /// (eraghi)13
 /// Extended CheckedListBox with binding facilities (Value property)
    /// Extended CheckedListBox with binding facilities (Value property)14
 /// </summary>
    /// </summary>15
 [ToolboxBitmap(typeof(CheckedListBox))]
    [ToolboxBitmap(typeof(CheckedListBox))]16
 public class ExCheckedListBox : CheckedListBox
    public class ExCheckedListBox : CheckedListBox17
 {
    {18
 /// <summary>
        /// <summary>19
 /// Default constructor
        /// Default constructor20
 /// </summary>
        /// </summary>21
 public ExCheckedListBox()
        public ExCheckedListBox()22
 {
        {23
 this.CheckOnClick = true;
            this.CheckOnClick = true;24
 
           25
 }
        }26

27
 
       28
 
        29
 /// <summary>
        /// <summary>30
 ///    Gets or sets the property to display for this CustomControls.CheckedListBox.
        ///    Gets or sets the property to display for this CustomControls.CheckedListBox.31
 ///
        ///32
 /// Returns:
        /// Returns:33
 ///     A System.String specifying the name of an object property that is contained
        ///     A System.String specifying the name of an object property that is contained34
 ///     in the collection specified by the CustomControls.CheckedListBox.DataSource
        ///     in the collection specified by the CustomControls.CheckedListBox.DataSource35
 ///     property. The default is an empty string ("").
        ///     property. The default is an empty string ("").36
 /// </summary>
        /// </summary>37
 [DefaultValue("")]
        [DefaultValue("")]38
 [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [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))]
        [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]40
 [Browsable(true)]
        [Browsable(true)]41
 public new string DisplayMember
        public new string DisplayMember42
 {
        {43
 get
            get44
 {
            {45
 return base.DisplayMember;
                return base.DisplayMember;46
 }
            }47
 set
            set48
 {
            {49
 base.DisplayMember = value;
                base.DisplayMember = value;50
 
                51
 }
            }52
 }
        }53
 
       54
 /// <summary>
        /// <summary>55
 /// Gets or sets the data source for this CustomControls.CheckedListBox.
        /// Gets or sets the data source for this CustomControls.CheckedListBox.56
 /// Returns:
        /// Returns:57
 ///    An object that implements the System.Collections.IList or System.ComponentModel.IListSource
        ///    An object that implements the System.Collections.IList or System.ComponentModel.IListSource58
 ///    interfaces, such as a System.Data.DataSet or an System.Array. The default
        ///    interfaces, such as a System.Data.DataSet or an System.Array. The default59
 ///    is null.
        ///    is null.60
 ///
        ///61
 ///Exceptions:
        ///Exceptions:62
 ///  System.ArgumentException:
        ///  System.ArgumentException:63
 ///    The assigned value does not implement the System.Collections.IList or System.ComponentModel.IListSource
        ///    The assigned value does not implement the System.Collections.IList or System.ComponentModel.IListSource64
 ///    interfaces.
        ///    interfaces.65
 /// </summary>
        /// </summary>66
 [DefaultValue("")]
        [DefaultValue("")]67
 [AttributeProvider(typeof(IListSource))]
        [AttributeProvider(typeof(IListSource))]68
 [RefreshProperties(RefreshProperties.All)]
        [RefreshProperties(RefreshProperties.All)]69
 [Browsable(true)]
        [Browsable(true)]70
 public new object DataSource {
        public new object DataSource { 71
 get
            get 72
 {
            {73
 return base.DataSource;
                return base.DataSource;74
 }
            }75
 set
            set 76
 {
            {77
 base.DataSource = value;
                base.DataSource = value;78
 
                79
 }
            }80
 }
        }81
 private int value;
        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))]
        [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)]
        [Browsable(true)]84
 public new string ValueMember
        public new string ValueMember85
 {
        {86
 get
              get87
 {
            {88
 ///Gets checked items in decimal mode from binary mode
                ///Gets checked items in decimal mode from binary mode89

90
 try
                try91
 {
                {92
 //each item in list has a number that is binary number in decimal mode
                    //each item in list has a number that is binary number in decimal mode93
 //this number represents that number
                    //this number represents that number94
 int poweredNumber = 1;
                    int poweredNumber = 1;95
 //loop in all items of list
                    //loop in all items of list96
 for (int i = 0; i < this.Items.Count; i++)
                    for (int i = 0; i < this.Items.Count; i++)97
 {
                    {98
 //if item checked and the value doesn't contains poweredNumber then
                        //if item checked and the value doesn't contains poweredNumber then99
 //add poweredNumber to the value
                        //add poweredNumber to the value100
 if ((this.GetItemChecked(i)))
                        if ((this.GetItemChecked(i)))101
 this.value |= poweredNumber;
                            this.value |= poweredNumber;102
 //else if poweredNumber exists in the value remove from it
                        //else if poweredNumber exists in the value remove from it103
 else if ((this.value & poweredNumber) != 0)
                        else if ((this.value & poweredNumber) != 0)104
 this.value -= poweredNumber;
                            this.value -= poweredNumber;105

106
 //raise to the power
                        //raise to the power107
 poweredNumber *= 2;
                        poweredNumber *= 2;108
 }
                    }109
 }
                }110
 catch (ArgumentException ex)
                catch (ArgumentException ex)111
 {
                {112
 throw ex;
                    throw ex;113
 }
                }114
 catch (Exception ex)
                catch (Exception ex)115
 {
                {116
 throw ex;
                    throw ex;117
 }
                }118

119

120
 return base.ValueMember;
                return base.ValueMember;121
 }
            }122
 set
            set123
 {
            {124
 base.ValueMember = value;
                base.ValueMember = value;125
 if (base.ValueMember.ToLower() == "false")
                if (base.ValueMember.ToLower() == "false")126
 this.value = 0;
                    this.value = 0;127
 else
                else128
 this.value = 1;
                    this.value = 1;129

130
 try
                try131
 {
                {132
 //each item in list has a number that is binary number in decimal mode
                    //each item in list has a number that is binary number in decimal mode133
 //this number represents that number
                    //this number represents that number134
 int poweredNumber = 1;
                    int poweredNumber = 1;135
 //loop in all items of list
                    //loop in all items of list136
 for (int i = 0; i < this.Items.Count; i++)
                    for (int i = 0; i < this.Items.Count; i++)137
 {
                    {138
 //if poweredNumber exists in the value set checked on item
                        //if poweredNumber exists in the value set checked on item139
 if ((this.value & poweredNumber) != 0)
                        if ((this.value & poweredNumber) != 0)140
 this.SetItemCheckState(i, CheckState.Checked);
                            this.SetItemCheckState(i, CheckState.Checked);141
 //else remove checked from item
                        //else remove checked from item142
 else
                        else143
 this.SetItemCheckState(i, CheckState.Unchecked);
                            this.SetItemCheckState(i, CheckState.Unchecked);144

145
 //raise to the power
                        //raise to the power146
 poweredNumber *= 2;
                        poweredNumber *= 2;147
 }
                    }148
 }
                }149
 catch (ArgumentException ex)
                catch (ArgumentException ex)150
 {
                {151
 throw ex;
                    throw ex;152
 }
                }153
 catch (Exception ex)
                catch (Exception ex)154
 {
                {155
 throw ex;
                    throw ex;156
 }
                }157
 }
            }158
 }
        }159
 }
     }160
 }
}161

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

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号