Csharp:a Custom CheckedListBox with Datasource

Source: Internet
Author: User

 <summary>//(Eraghi)//Custom CheckedListBox with binding facilities (Value property)///from A C Ustom CheckedListBox with Datasource http://www.codeproject.com/Articles/22960/ A-custom-checkedlistbox-with-datasource-implementa//</summary> [ToolboxBitmap (typeof (CheckedListBox))] P Ublic class Ducheckedlistbox:checkedlistbox {//<summary>//Default constructor//&L T;/summary> public Ducheckedlistbox () {this.        CheckOnClick = true;        }//<summary>//sets the property to display for this customcontrols.checkedlistbox.        Returns://A System.String specifying the name of an object property which 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 Displayme mber {get {return base.            DisplayMember; } set {base.            DisplayMember = value; }}///<summary>//sets the property to get the values for this customcontrols.ch        Eckedlistbox.        Returns://A System.String specifying the name of an object property which is contained In the collection specified by the CustomControls.CheckedListBox.DataSource//property. The default is anEmpty 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 VALUEMEMB Er {get {return base.            ValueMember; } set {base.            ValueMember = value;        }}///<summary>//sets the data source for this customcontrols.checkedlistbox.         Returns://An object, 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 Sy Stem.        Collections.ilist or System.ComponentModel.IListSource///interfaces. </summary> [DefaultValue ("")] [Attributeprovider (typeof (IListSource))] [Refreshproperties (R                Efreshproperties.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 value             s ID///</summary> [Bindable (True), browsable (true)] public list<int> ValueList { Get {///gets checked items ID values in a list list<int> Retar Ray= 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& Gt                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 Ju Wen"); Dt.            Rows.Add (2, "Geovin Du"); Dt.            Rows.Add (3, "Geovindu"); Dt.            Rows.Add (4, "Graffiti Kingdom"); Dt.            Rows.Add (5, "Tu Shi"); Dt.            Rows.Add (6, "Zhang Shi"); Dt.            Rows.Add (7, "Kuok"); Dt.            Rows.Add (8, "Jiang Shi");        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";            set 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 of the other item ' is ' to ' 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; Select for all//while (Myenumerator.movenext ()! = False)//{//y = (int) Myenumerator.curr            Ent            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 (Itemche cked)).            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 () + "."); }        }

  

Csharp:a Custom CheckedListBox with Datasource

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.