Csharp: A Custom CheckedListBox with Datasource, checkedlistbox

Source: Internet
Author: User

Csharp: A Custom CheckedListBox with Datasource, checkedlistbox

 /// <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);                    }                }            }        }    }

Test:

DataTable setData () {DataTable dt = new DataTable (); dt. columns. add ("ID", typeof (int); dt. columns. add ("Name", typeof (string); dt. rows. add (1, "tu juwen"); dt. rows. add (2, "Geovin Du"); dt. rows. add (3, "geovindu"); dt. rows. add (4, "graffiti kingdom"); dt. rows. add (5, "tu"); dt. rows. add (6, "Zhang"); dt. rows. add (7, "Guo"); dt. rows. add (8, "Jiang"); return dt ;}/// <summary> //// </summary> public CheckedlistboxForm () {Init IalizeComponent ();} /// <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"; // set bool insideCheckEveryOther = true; for (int I = 0; I <duCheckedListBox1. I Tems. 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 button#click (object sender, EventArgs e) {IEnumerator myEnumerator; myEnumerator = duCheckedListBox1.CheckedIndices. getEnumerator (); int y; // select none for all. // 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 () + ". ");}}

Set selected items:

            //2.            List<int> list;            list = new List<int> { 1, 4 };            int value = 0;            //for (int i = 0; i < duCheckedListBox1.Items.Count; i++)            //{            //    DataRowView view = duCheckedListBox1.Items[i] as DataRowView;            //    value = (int)view["ID"];            //    if (list.Contains(value))            //        duCheckedListBox1.SetItemChecked(i, true);            //}            for (int i = 0; i < duCheckedListBox1.Items.Count; i++)            {                DataRowView view = duCheckedListBox1.Items[i] as DataRowView;                value = (int)view["ID"];                if (value == 5)                    duCheckedListBox1.SetItemChecked(i, true);            }

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.