改进的CheckedListBox控件

这个话题似乎很早就有说了,
不过我也最近才经常接触的winform。记录之

我希望能够在CheckedListBox中实现如下的效果:
改进的CheckedListBox控件改进的CheckedListBox控件
 1改进的CheckedListBox控件 1        Category.CategorysDataTable dt;            dt = new Category.CategorysDataTable();
 2改进的CheckedListBox控件 2            dt.Constraints.Add(new UniqueConstraint(dt.IDColumn));
 3改进的CheckedListBox控件 3            dt.Constraints.Add(new UniqueConstraint(dt.TableNameColumn));
 4改进的CheckedListBox控件 4            Category.CategorysRow row = dt.NewCategorysRow();
 5改进的CheckedListBox控件 5            row.ID = 1;
 6改进的CheckedListBox控件 6            row.OrginialSequence = 1;
 7改进的CheckedListBox控件 7            row.TargetSequence = 1;
 8改进的CheckedListBox控件 8            row.TableName = "TB";
 9改进的CheckedListBox控件 9            row.Flag = true;
10改进的CheckedListBox控件10            dt.AddCategorysRow(row);
11改进的CheckedListBox控件11
12改进的CheckedListBox控件12            row = dt.NewCategorysRow();
13改进的CheckedListBox控件13            row.ID = 2;
14改进的CheckedListBox控件14            row.OrginialSequence = 1;
15改进的CheckedListBox控件15            row.TargetSequence = 1;
16改进的CheckedListBox控件16            row.TableName = "TB2";
17改进的CheckedListBox控件17            row.Flag = false;
18改进的CheckedListBox控件18            dt.AddCategorysRow(row);
19改进的CheckedListBox控件            this.chklstbxTables.Items.Clear();
20改进的CheckedListBox控件            this.chklstbxTables.DataSource = dt;
21改进的CheckedListBox控件            this.chklstbxTables.DisplayMember = "TableName";
22改进的CheckedListBox控件            this.chklstbxTables.ValueMember= "Flag";
通过它,可以将CheckedListBox的复选框绑定到一个数据库的boolean型的字段上,效果如下

改进的CheckedListBox控件
下面是我改造的过程

首先查了一下msdn,
http://msdn2.microsoft.com/zh-cn/library/system.windows.forms.checkedlistbox_members(VS.80).aspx
会看到如下几个属性,
改进的CheckedListBox控件 DataSource 获取或设置控件的数据源。此属性与此类无关。
改进的CheckedListBox控件 DisplayMember 此属性与此类无关。
改进的CheckedListBox控件 ValueMember 获取或设置一个字符串,该字符串指定要从中取值的数据源的属性此属性与此类无关。
但是这三个属性是我们需要的,
使用Reflector查看了一CheckedListBox的关系改进的CheckedListBox控件
在CheckedListBox中本身已经实现了这三个属性,仅是ms使用了如下的特性,使我们不能用它了,[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
知道了来龙去脉就好改造了,在原CheckedListBox基础上再扩展一个类ExCheckedListBox
  1改进的CheckedListBox控件using System;
  2改进的CheckedListBox控件using System.Collections.Generic;
  3改进的CheckedListBox控件using System.Text;
  4改进的CheckedListBox控件using System.ComponentModel;
  5改进的CheckedListBox控件using System.Drawing;
  6改进的CheckedListBox控件using System.Windows.Forms;
  7改进的CheckedListBox控件using System.Drawing.Design;
  8改进的CheckedListBox控件
  9改进的CheckedListBox控件namespace CustomControls
 10改进的CheckedListBox控件改进的CheckedListBox控件改进的CheckedListBox控件{
 11改进的CheckedListBox控件改进的CheckedListBox控件    /**//// <summary>
 12改进的CheckedListBox控件    /// (eraghi)
 13改进的CheckedListBox控件    /// Extended CheckedListBox with binding facilities (Value property)
 14改进的CheckedListBox控件    /// </summary>

 15改进的CheckedListBox控件    [ToolboxBitmap(typeof(CheckedListBox))]
 16改进的CheckedListBox控件    public class ExCheckedListBox : CheckedListBox
 17改进的CheckedListBox控件改进的CheckedListBox控件    改进的CheckedListBox控件{
 18改进的CheckedListBox控件改进的CheckedListBox控件        /**//// <summary>
 19改进的CheckedListBox控件        /// Default constructor
 20改进的CheckedListBox控件        /// </summary>

 21改进的CheckedListBox控件        public ExCheckedListBox()
 22改进的CheckedListBox控件改进的CheckedListBox控件        改进的CheckedListBox控件{
 23改进的CheckedListBox控件            this.CheckOnClick = true;
 24改进的CheckedListBox控件           
 25改进的CheckedListBox控件        }

 26改进的CheckedListBox控件
 27改进的CheckedListBox控件       
 28改进的CheckedListBox控件        
 29改进的CheckedListBox控件改进的CheckedListBox控件        /**//// <summary>
 30改进的CheckedListBox控件        ///    Gets or sets the property to display for this CustomControls.CheckedListBox.
 31改进的CheckedListBox控件        ///
 32改进的CheckedListBox控件        /// Returns:
 33改进的CheckedListBox控件        ///     A System.String specifying the name of an object property that is contained
 34改进的CheckedListBox控件        ///     in the collection specified by the CustomControls.CheckedListBox.DataSource
 35改进的CheckedListBox控件        ///     property. The default is an empty string ("").
 36改进的CheckedListBox控件        /// </summary>

 37改进的CheckedListBox控件        [DefaultValue("")]
 38改进的CheckedListBox控件        [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
 39改进的CheckedListBox控件        [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"typeof(UITypeEditor))]
 40改进的CheckedListBox控件        [Browsable(true)]
 41改进的CheckedListBox控件        public new string DisplayMember
 42改进的CheckedListBox控件改进的CheckedListBox控件        改进的CheckedListBox控件{
 43改进的CheckedListBox控件            get
 44改进的CheckedListBox控件改进的CheckedListBox控件            改进的CheckedListBox控件{
 45改进的CheckedListBox控件                return base.DisplayMember;
 46改进的CheckedListBox控件            }

 47改进的CheckedListBox控件            set
 48改进的CheckedListBox控件改进的CheckedListBox控件            改进的CheckedListBox控件{
 49改进的CheckedListBox控件                base.DisplayMember = value;
 50改进的CheckedListBox控件                
 51改进的CheckedListBox控件            }

 52改进的CheckedListBox控件        }

 53改进的CheckedListBox控件       
 54改进的CheckedListBox控件改进的CheckedListBox控件        /**//// <summary>
 55改进的CheckedListBox控件        /// Gets or sets the data source for this CustomControls.CheckedListBox.
 56改进的CheckedListBox控件        /// Returns:
 57改进的CheckedListBox控件        ///    An object that implements the System.Collections.IList or System.ComponentModel.IListSource
 58改进的CheckedListBox控件        ///    interfaces, such as a System.Data.DataSet or an System.Array. The default
 59改进的CheckedListBox控件        ///    is null.
 60改进的CheckedListBox控件        ///
 61改进的CheckedListBox控件        ///Exceptions:
 62改进的CheckedListBox控件        ///  System.ArgumentException:
 63改进的CheckedListBox控件        ///    The assigned value does not implement the System.Collections.IList or System.ComponentModel.IListSource
 64改进的CheckedListBox控件        ///    interfaces.
 65改进的CheckedListBox控件        /// </summary>

 66改进的CheckedListBox控件        [DefaultValue("")]
 67改进的CheckedListBox控件        [AttributeProvider(typeof(IListSource))]
 68改进的CheckedListBox控件        [RefreshProperties(RefreshProperties.All)]
 69改进的CheckedListBox控件        [Browsable(true)]
 70改进的CheckedListBox控件改进的CheckedListBox控件        public new object DataSource 改进的CheckedListBox控件
 71改进的CheckedListBox控件            get 
 72改进的CheckedListBox控件改进的CheckedListBox控件            改进的CheckedListBox控件{
 73改进的CheckedListBox控件                return base.DataSource;
 74改进的CheckedListBox控件            }

 75改进的CheckedListBox控件            set 
 76改进的CheckedListBox控件改进的CheckedListBox控件            改进的CheckedListBox控件{
 77改进的CheckedListBox控件                base.DataSource = value;
 78改进的CheckedListBox控件                
 79改进的CheckedListBox控件            }

 80改进的CheckedListBox控件        }

 81改进的CheckedListBox控件        private int value;
 82改进的CheckedListBox控件        [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改进的CheckedListBox控件        [Browsable(true)]
 84改进的CheckedListBox控件        public new string ValueMember
 85改进的CheckedListBox控件改进的CheckedListBox控件        改进的CheckedListBox控件{
 86改进的CheckedListBox控件              get
 87改进的CheckedListBox控件改进的CheckedListBox控件            改进的CheckedListBox控件{
 88改进的CheckedListBox控件改进的CheckedListBox控件                /**////Gets checked items in decimal mode from binary mode
 89改进的CheckedListBox控件
 90改进的CheckedListBox控件                try
 91改进的CheckedListBox控件改进的CheckedListBox控件                改进的CheckedListBox控件{
 92改进的CheckedListBox控件                    //each item in list has a number that is binary number in decimal mode
 93改进的CheckedListBox控件                    //this number represents that number
 94改进的CheckedListBox控件                    int poweredNumber = 1;
 95改进的CheckedListBox控件                    //loop in all items of list
 96改进的CheckedListBox控件                    for (int i = 0; i < this.Items.Count; i++)
 97改进的CheckedListBox控件改进的CheckedListBox控件                    改进的CheckedListBox控件{
 98改进的CheckedListBox控件                        //if item checked and the value doesn't contains poweredNumber then
 99改进的CheckedListBox控件                        //add poweredNumber to the value
100改进的CheckedListBox控件                        if ((this.GetItemChecked(i)))
101改进的CheckedListBox控件                            this.value |= poweredNumber;
102改进的CheckedListBox控件                        //else if poweredNumber exists in the value remove from it
103改进的CheckedListBox控件                        else if ((this.value & poweredNumber) != 0)
104改进的CheckedListBox控件                            this.value -= poweredNumber;
105改进的CheckedListBox控件
106改进的CheckedListBox控件                        //raise to the power
107改进的CheckedListBox控件                        poweredNumber *= 2;
108改进的CheckedListBox控件                    }

109改进的CheckedListBox控件                }

110改进的CheckedListBox控件                catch (ArgumentException ex)
111改进的CheckedListBox控件改进的CheckedListBox控件                改进的CheckedListBox控件{
112改进的CheckedListBox控件                    throw ex;
113改进的CheckedListBox控件                }

114改进的CheckedListBox控件                catch (Exception ex)
115改进的CheckedListBox控件改进的CheckedListBox控件                改进的CheckedListBox控件{
116改进的CheckedListBox控件                    throw ex;
117改进的CheckedListBox控件                }

118改进的CheckedListBox控件
119改进的CheckedListBox控件
120改进的CheckedListBox控件                return base.ValueMember;
121改进的CheckedListBox控件            }

122改进的CheckedListBox控件            set
123改进的CheckedListBox控件改进的CheckedListBox控件            改进的CheckedListBox控件{
124改进的CheckedListBox控件                base.ValueMember = value;
125改进的CheckedListBox控件                if (base.ValueMember.ToLower() == "false")
126改进的CheckedListBox控件                    this.value = 0;
127改进的CheckedListBox控件                else
128改进的CheckedListBox控件                    this.value = 1;
129改进的CheckedListBox控件
130改进的CheckedListBox控件                try
131改进的CheckedListBox控件改进的CheckedListBox控件                改进的CheckedListBox控件{
132改进的CheckedListBox控件                    //each item in list has a number that is binary number in decimal mode
133改进的CheckedListBox控件                    //this number represents that number
134改进的CheckedListBox控件                    int poweredNumber = 1;
135改进的CheckedListBox控件                    //loop in all items of list
136改进的CheckedListBox控件                    for (int i = 0; i < this.Items.Count; i++)
137改进的CheckedListBox控件改进的CheckedListBox控件                    改进的CheckedListBox控件{
138改进的CheckedListBox控件                        //if poweredNumber exists in the value set checked on item
139改进的CheckedListBox控件                        if ((this.value & poweredNumber) != 0)
140改进的CheckedListBox控件                            this.SetItemCheckState(i, CheckState.Checked);
141改进的CheckedListBox控件                        //else remove checked from item
142改进的CheckedListBox控件                        else
143改进的CheckedListBox控件                            this.SetItemCheckState(i, CheckState.Unchecked);
144改进的CheckedListBox控件
145改进的CheckedListBox控件                        //raise to the power
146改进的CheckedListBox控件                        poweredNumber *= 2;
147改进的CheckedListBox控件                    }

148改进的CheckedListBox控件                }

149改进的CheckedListBox控件                catch (ArgumentException ex)
150改进的CheckedListBox控件改进的CheckedListBox控件                改进的CheckedListBox控件{
151改进的CheckedListBox控件                    throw ex;
152改进的CheckedListBox控件                }

153改进的CheckedListBox控件                catch (Exception ex)
154改进的CheckedListBox控件改进的CheckedListBox控件                改进的CheckedListBox控件{
155改进的CheckedListBox控件                    throw ex;
156改进的CheckedListBox控件                }

157改进的CheckedListBox控件            }

158改进的CheckedListBox控件        }

159改进的CheckedListBox控件     }

160改进的CheckedListBox控件}

161改进的CheckedListBox控件
其中控件的主要代码,这位伊郞大哥的code,link如下:
http://www.codeproject.com/cs/combobox/ExCheckedListBox.asp
posted on 2007-11-29 17:12 lexus 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2007/11/29/813740.html