csharp:A Custom CheckedListBox with Datasource

 /// <summary>
    /// (eraghi)
    /// Custom CheckedListBox with binding facilities (Value property)
    /// from A Custom CheckedListBox with Datasource  http://www.codeproject.com/Articles/22960/A-Custom-CheckedListBox-with-Datasource-Implementa
    /// </summary>
    [ToolboxBitmap(typeof(CheckedListBox))]
    public class DuCheckedListBox : CheckedListBox
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public DuCheckedListBox()
        {
            this.CheckOnClick = true;

        }

        /// <summary>
        ///    Gets or sets the property to display for this CustomControls.CheckedListBox.
        ///
        /// Returns:
        ///     A System.String specifying the name of an object property that is contained
        ///     in the collection specified by the CustomControls.CheckedListBox.DataSource
        ///     property. The default is an empty string ("").
        /// </summary>
        [DefaultValue("")]
        [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=3de65a4a-2b5f-4d9d-88de-bfb692b10f93")]
        [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=3de65a4a-2b5f-4d9d-88de-bfb692b10f93", typeof(UITypeEditor))]
        [Browsable(true)]
        public new string DisplayMember
        {
            get
            {
                return base.DisplayMember;
            }
            set
            {
                base.DisplayMember = value;

            }
        }

        /// <summary>
        ///    Gets or sets the property to get the values for this CustomControls.CheckedListBox.
        ///
        /// Returns:
        ///     A System.String specifying the name of an object property that is contained
        ///     in the collection specified by the CustomControls.CheckedListBox.DataSource
        ///     property. The default is an empty string ("").
        /// </summary>
        [DefaultValue("")]
        [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=3de65a4a-2b5f-4d9d-88de-bfb692b10f93")]
        [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=3de65a4a-2b5f-4d9d-88de-bfb692b10f93", typeof(UITypeEditor))]
        [Browsable(true)]
        public new string ValueMember
        {
            get
            {
                return base.ValueMember;
            }
            set
            {
                base.ValueMember = value;

            }
        }

        /// <summary>
        /// Gets or sets the data source for this CustomControls.CheckedListBox.
        /// Returns:
        ///    An object that implements the System.Collections.IList or System.ComponentModel.IListSource
        ///    interfaces, such as a System.Data.DataSet or an System.Array. The default
        ///    is null.
        ///
        ///Exceptions:
        ///  System.ArgumentException:
        ///    The assigned value does not implement the System.Collections.IList or System.ComponentModel.IListSource
        ///    interfaces.
        /// </summary>
        [DefaultValue("")]
        [AttributeProvider(typeof(IListSource))]
        [RefreshProperties(RefreshProperties.All)]
        [Browsable(true)]
        public new object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                base.DataSource = value;

            }
        }

        /// <summary>
        /// Gets and sets an integer array of the values based on checked items values ID
        /// </summary>
        [Bindable(true), Browsable(true)]
        public List<int> ValueList
        {
            get
            {
                ///Gets checked items id values in a list
                List<int> retArray = new List<int>();
                PropertyDescriptor prop = null;
                PropertyDescriptorCollection propList = this.DataManager.GetItemProperties();
                prop = propList.Find(this.ValueMember, false);
                object checkedItem;
                if (prop != null)
                {
                    for (int i = 0; i < this.Items.Count; i++)
                    {
                        if (this.GetItemChecked(i))
                        {
                            checkedItem = this.DataManager.List[i];
                            retArray.Add(Convert.ToInt32(prop.GetValue(checkedItem).ToString()));
                        }
                    }
                }
                return retArray;
            }

            set
            {
                ///Sets checked items base on id values in a list
                List<int> myList = value;
                PropertyDescriptor prop = null;
                PropertyDescriptorCollection propList = this.DataManager.GetItemProperties();
                prop = propList.Find(this.ValueMember, false);
                object checkedItem;
                int intValItem;
                int found;
                if (prop != null)
                {
                    for (int i = 0; i < this.Items.Count; i++)
                    {
                        checkedItem = this.DataManager.List[i];
                        intValItem = Convert.ToInt32(prop.GetValue(checkedItem).ToString());
                        found = (from c in myList where c == intValItem select c).Count();
                        if (found == 1)
                            this.SetItemCheckState(i, CheckState.Checked);
                        else
                            this.SetItemCheckState(i, CheckState.Unchecked);
                    }
                }
            }
        }
    }

  测试:

        DataTable setData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Rows.Add(1, "涂聚文");
            dt.Rows.Add(2, "Geovin Du");
            dt.Rows.Add(3, "geovindu");
            dt.Rows.Add(4, "涂鸦王国");
            dt.Rows.Add(5, "涂氏");
            dt.Rows.Add(6, "张氏");
            dt.Rows.Add(7, "郭氏");
            dt.Rows.Add(8, "江氏");
            return dt;
        }

        /// <summary>
        ///
        /// </summary>
        public CheckedlistboxForm()
        {
            InitializeComponent();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckedlistboxForm_Load(object sender, EventArgs e)
        {
            this.duCheckedListBox1.DataSource = setData();
            this.duCheckedListBox1.DisplayMember = "Name";
            this.duCheckedListBox1.ValueMember = "ID";
            //设定
            bool  insideCheckEveryOther = true;
            for (int i = 0; i < duCheckedListBox1.Items.Count; i++)
            {
                // For every other item in the list, set as checked.
                if ((i % 2) == 0)
                {
                    // But for each other item that is to be checked, set as being in an
                    // indeterminate checked state.
                    if ((i % 4) == 0)
                        duCheckedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
                    else
                        duCheckedListBox1.SetItemChecked(i, true);
                }
            }
            insideCheckEveryOther = false;

        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            IEnumerator myEnumerator;
            myEnumerator = duCheckedListBox1.CheckedIndices.GetEnumerator();
            int y;
            //选择为全为无选
            //while (myEnumerator.MoveNext() != false)
            //{
            //    y = (int)myEnumerator.Current;
            //    duCheckedListBox1.SetItemChecked(y, false);
            //}

            //foreach (object itemChecked in duCheckedListBox1.CheckedItems)
            //{
            //    MessageBox.Show("Item with title: \"" + itemChecked.ToString() +
            //            "\", is checked. Checked state is: " +
            //            duCheckedListBox1.GetItemCheckState(duCheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
            //}

            foreach (DataRowView itemChecked in duCheckedListBox1.CheckedItems)
            {
                MessageBox.Show("Item with title: \"" + itemChecked[0].ToString() + itemChecked[1].ToString() +
                        "\", is checked. Checked state is: " +
                        duCheckedListBox1.GetItemCheckState(duCheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
            }
        }

  

时间: 2024-10-12 21:38:59

csharp:A Custom CheckedListBox with Datasource的相关文章

WinForm之BindingSource基础操作实例教程

通常我们在进行数据绑定的时候,常用的数据源有DataSet.DataTable.BindingList<T>.还有强类型数据源.今天我们来通过实例了解一下BindingSource组建,分享给大家供大家参考借鉴之用. BindingSource的两个用途: (1)首先,它提供一个将窗体上的控件绑定到数据的间接层.这是通过将 BindingSource 组件绑定到数据源,然后将窗体上的控件绑定到 BindingSource 组件来完成的.与数据的所有进一步交互(包括导航.排序.筛选和更新)都是通

SAP BW create custom Datasource

用到的TCODE: SE37/SE80函数构建器 SE11 ABAP字典(包括数据库表/视图/数据类型等) SE16 数据浏览器 RSA6 数据源目录 RSO2 维护一般数据源(事务数据/主数据属性/文本) RSA3 提取器检验程序(数据源测试) SE80 新建函数组,利用copy RSAX新建一个,最好一个函数组对应一个数据源 安全间隔下限:若2,则会将今天+昨天+前天的数抽一次 数据库表:ZHBSJ 数据类型:ZDS_ZHBSJ_STR 函数组:ZDS_ZHBSJ_GRP 函数:ZDS_ZH

CheckedListBox&#160; 数据绑定

CheckedListBox  数据绑定方式有多总,常用的绑定方式总结如下: 1.  Items.Add 通过 Itemes.Add 方法来向 CheckedListBox  中添加项 2. DataSource属性,DisplayMember和ValueMemeber 来设置文本显示 CheckedListBox 和 ListBox  相似,但 VS 的智能感应窗口并不提示 DataSoource 属性及 DisplayMember 和 ValueMember , 但这些属性是实际存在并可用的

checkedListBox的数据绑定

string strq = "select Stru_chName,Stru_enName from T_StuRowName;"; SqlCommand command = new SqlCommand(strq, con); SqlDataReader reader = command.ExecuteReader(); BindingSource bs = new BindingSource(); bs.DataSource = reader; checkedListBox1.Da

csharp: get Web.Services WebMethod

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services.Description; using System.Web.Services.Protocols; using System.Web.Services.Discovery; using System.Web.Services; using System.Net; using S

[DataSource] Properties 以及 DataSource 各个选项的含义:Data Connection,Directory,Excel,File,Grid,Groovy,JDBC,XML

http://www.soapui.org/about-soapui-pro/working-with-soapui-pro.html DataSource type dropdown – lets you pick which external source you would use to pull into your test case. Excel – Point to an Excel (xls) file JDBC – Connect to a database and pull d

C# CheckedListBox控件的使用方法

1. 加入项 checkedListBox1.Items.Add("蓝色"); checkedListBox1.Items.Add("红色"); checkedListBox1.Items.Add("黄色"); 2. 推断第i项是否选中,选中为true,否则为false if(checkedListBox1.GetItemChecked(i)) {      return true; } else {      return false; } 3

CSharp 连接本地SQL Server 数据库

本文将详细介绍如何使用Connection对象连接数据库.对于不同的.NET数据提供者,ADO.NET采用不同的Connection对象连接数据库.这些Connection对象为我们屏蔽了具体的实现细节,并提供了一种统一的实现方法. Connection类有四种:SqlConnection,OleDbConnection,OdbcConnection和OracleConnection. SqlConnection类的对象连接SQL Server数据库:OracleConnection 类的对象连

WinForm(C#)CheckedlistBox绑定数据,并获得选中的值(ValueMember)和显示文本(DisplayMember)

本文中我将和大家讨论关于在WinForm开发中给CheckedlistBox空间绑定数据源,并获取控件中选中的所有元素的显示文本(DisplayMember)和对应的实际值(ValueMember)的问题,后者将是讨论的重点. 为了更方便地说明,首先我要预设一些条件. 条件预设: 1.已定义一个DataTable对象myDataTable,并且myDataTable的字段及数据如下: ID 分类名称(TypeName) 1 金属制品 2 通用及专用机械设备 3 纸及纸制品 4 交通运输设备 5