改进的CheckedListBox控件

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

我希望能够在CheckedListBox中实现如下的效果:
ContractedBlock.gif ExpandedBlockStart.gif
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1None.gif 1        Category.CategorysDataTable dt;            dt = new Category.CategorysDataTable();
 2None.gif 2            dt.Constraints.Add(new UniqueConstraint(dt.IDColumn));
 3None.gif 3            dt.Constraints.Add(new UniqueConstraint(dt.TableNameColumn));
 4None.gif 4            Category.CategorysRow row = dt.NewCategorysRow();
 5None.gif 5            row.ID = 1;
 6None.gif 6            row.OrginialSequence = 1;
 7None.gif 7            row.TargetSequence = 1;
 8None.gif 8            row.TableName = "TB";
 9None.gif 9            row.Flag = true;
10None.gif10            dt.AddCategorysRow(row);
11None.gif11
12None.gif12            row = dt.NewCategorysRow();
13None.gif13            row.ID = 2;
14None.gif14            row.OrginialSequence = 1;
15None.gif15            row.TargetSequence = 1;
16None.gif16            row.TableName = "TB2";
17None.gif17            row.Flag = false;
18None.gif18            dt.AddCategorysRow(row);
19None.gif            this.chklstbxTables.Items.Clear();
20None.gif            this.chklstbxTables.DataSource = dt;
21None.gif            this.chklstbxTables.DisplayMember = "TableName";
22None.gif            this.chklstbxTables.ValueMember= "Flag";
通过它,可以将CheckedListBox的复选框绑定到一个数据库的boolean型的字段上,效果如下


下面是我改造的过程

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

 15InBlock.gif    [ToolboxBitmap(typeof(CheckedListBox))]
 16InBlock.gif    public class ExCheckedListBox : CheckedListBox
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 19InBlock.gif        /// Default constructor
 20ExpandedSubBlockEnd.gif        /// </summary>

 21InBlock.gif        public ExCheckedListBox()
 22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 23InBlock.gif            this.CheckOnClick = true;
 24InBlock.gif           
 25ExpandedSubBlockEnd.gif        }

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

 37InBlock.gif        [DefaultValue("")]
 38InBlock.gif        [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
 39InBlock.gif        [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"typeof(UITypeEditor))]
 40InBlock.gif        [Browsable(true)]
 41InBlock.gif        public new string DisplayMember
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 43InBlock.gif            get
 44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 45InBlock.gif                return base.DisplayMember;
 46ExpandedSubBlockEnd.gif            }

 47InBlock.gif            set
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 49InBlock.gif                base.DisplayMember = value;
 50InBlock.gif                
 51ExpandedSubBlockEnd.gif            }

 52ExpandedSubBlockEnd.gif        }

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

 66InBlock.gif        [DefaultValue("")]
 67InBlock.gif        [AttributeProvider(typeof(IListSource))]
 68InBlock.gif        [RefreshProperties(RefreshProperties.All)]
 69InBlock.gif        [Browsable(true)]
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        public new object DataSource dot.gif
 71InBlock.gif            get 
 72ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 73InBlock.gif                return base.DataSource;
 74ExpandedSubBlockEnd.gif            }

 75InBlock.gif            set 
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 77InBlock.gif                base.DataSource = value;
 78InBlock.gif                
 79ExpandedSubBlockEnd.gif            }

 80ExpandedSubBlockEnd.gif        }

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

109ExpandedSubBlockEnd.gif                }

110InBlock.gif                catch (ArgumentException ex)
111ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
112InBlock.gif                    throw ex;
113ExpandedSubBlockEnd.gif                }

114InBlock.gif                catch (Exception ex)
115ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
116InBlock.gif                    throw ex;
117ExpandedSubBlockEnd.gif                }

118InBlock.gif
119InBlock.gif
120InBlock.gif                return base.ValueMember;
121ExpandedSubBlockEnd.gif            }

122InBlock.gif            set
123ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
124InBlock.gif                base.ValueMember = value;
125InBlock.gif                if (base.ValueMember.ToLower() == "false")
126InBlock.gif                    this.value = 0;
127InBlock.gif                else
128InBlock.gif                    this.value 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值