zoukankan      html  css  js  c++  java
  • 改进的CheckedListBox控件

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

    我希望能够在CheckedListBox中实现如下的效果:
    通过它,可以将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
      1using System;
      2using System.Collections.Generic;
      3using System.Text;
      4using System.ComponentModel;
      5using System.Drawing;
      6using System.Windows.Forms;
      7using System.Drawing.Design;
      8
      9namespace 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如下:
    http://www.codeproject.com/cs/combobox/ExCheckedListBox.asp
  • 相关阅读:
    python之-- 异常
    实现Asp.Net Mvc4多级Views目录
    MVC控制下输出图片、javascript与json格式
    公共增删改查(MVC+三层架构)
    工厂方法模式
    简单工厂模式
    单例模式
    JavaScript正则表达式
    JavaScript对象与数组
    JavaScript数组排序
  • 原文地址:https://www.cnblogs.com/lexus/p/813740.html
Copyright © 2011-2022 走看看