C # checkedlistbox usage Summary

Source: Internet
Author: User

Generally, foreach (Object OBJ in checkedlistbox1.selecteditems) can traverse the selected value.

In fact, what we traverse here is that the highlighted value is not the checked value. Use the followingCode:

 

For(IntI =0; I <checkedlistbox1.items. Count; I ++){If(Checkedlistbox1.getitemchecked (I) {MessageBox. Show (checkedlistbox1.getitemtext (checkedlistbox1.items [I]);}

 

 

 

 Recently, I used the checklistbox control. It took a lot of time to use it. Here I collected the related code segments, hoping to help you.  1  . Add the checkedlistbox1.items. Add ("blue"); checkedlistbox1.items. Add ("red"); checkedlistbox1.items. Add ("yellow");  2  . Check whether the I-th item is selected. Select true. Otherwise, set false. If  (Checkedlistbox1.getitemchecked (I )){  Return   True  ;}  Else  {  Return   False  ;}  3  . Set whether checkedlistbox1.setitemchecked (I,  True ); //  If it is set to true or false, it is not selected.   4 . Select All to add a checkbox control named select_all, which controls whether to select all or not.  Private   Void Select_all_checkedchanged ( Object  Sender, eventargs e ){  If  (Select_all.checked ){  For ( Int J = 0 ; J <checkedlistbox1.items. Count; j ++ ) Checkedlistbox1.setitemchecked (J,  True );}  Else  {  For ( Int J = 0 ; J <checkedlistbox1.items. Count; j ++ ) Checkedlistbox1.setitemchecked (J,  False  );}}  5  . Obtain all selected values, and combine the text of the selected items into a string.  String Strcollected = String  . Empty; For ( Int I = 0 ; I <checkedlistbox1.items. Count; I ++ ){  If  (Checkedlistbox1.getitemchecked (I )){  If (Strcollected = String  . Empty) {strcollected = Checkedlistbox1.getitemtext (checkedlistbox1.items [I]);}  Else  {Strcollected = Strcollected + "/" + Checkedlistbox1.getitemtext (checkedlistbox1.items [I]) ;}}  6  . Set the checked status of item I in checkedlistbox checkedlistbox1.setitemcheckstate (I, checkstate. Checked );  7  .  Private   Void Checkboxall_checkedchanged ( Object  Sender, eventargs e ){  If  (Checkboxall. Checked ){  // If selected, all entries in the checkedlistbox are changed to the checked status.           For ( Int I = 0 ; I < Checkedlistboxlayercontrol. Items. Count; I ++ ) {Checkedlistboxlayercontrol. setitemcheckstate (I, checkstate. Checked );}}  Else  {  //  Otherwise, the status changes to unchecked.      For ( Int I =0  ; I <Checkedlistboxlayercontrol. Items. Count; I ++ ) {Checkedlistboxlayercontrol. setitemcheckstate (I, checkstate. Unchecked );}}}  8  . Checkedlistbox single-choice setting (code implementation)  Private   Void Chkl_itemauditing_itemcheck ( Object  Sender, itemcheckeventargs e ){  If (Chkl_itemauditing.checkeditems.count> 0  ){ For ( Int I = 0 ; I <chkl_itemauditing.items.count; I ++ ){  If (I! = E. Index ){  This  . Chkl_itemauditing.setitemcheckstate (I, system. Windows. Forms. checkstate. Unchecked );}}}}  9  . Checkedlistbox1 displays all records corresponding to keywords in a database  For ( Int I =0 ; I <Table. Rows. Count; I ++ ){  String Name = table. Rows [ "  Myname  "  ]. Tostring ();  String Paw = table. Rows [ "  Mypaw  "  ]. Tostring (); checkedlistbox1.items. Add (name + Paw );}  10 .  For (I = 0 ; I <checkedlistbox. Items. Count; I ++ ){  If  (Checkedlistbox. getitemtext (checkedlistbox. Items) = "The value you get") {checkedlistbox. setitemchecked (I,  True  );}}  11  . Clear all options in checkedlistbox1  For ( Int I =0 ; I <checkedlistbox1.items. Count; I ++ ) {Checkedlistbox1.items. Clear ();}  12  .  //  The items that set the index to index are selected.  For ( Int I = 0 ; I <checkedlistbox1.items. Count; I ++ ) {Checkedlistbox1.setitemchecked (I,  True  );}  13  . For ( Int I = 0 ; I <checkedlistbox1.items. Count; I ++ ){  If  (Checkedlistbox1.getselected (I) {MessageBox. Show (checkedlistbox1.checkeditems. tostring ());}}  14  .  //  Select all the options of checkedlistbox1   For ( Int I = 0 ; I <checkedlistbox1.items. Count; I ++ ) {Checkedlistbox1.setitemcheckstate (I, checkstate. Checked );}  15  .  For ( Int I = 0 ; I <checkedlistbox1.items. Count; I ++ ){  //  If the checkedlistbox1 entry I is selected,  //  The value corresponding to checkedlistbox1 is displayed.            If (Checkedlistbox1.getitemchecked (I) {MessageBox. Show (checkedlistbox1.items. tostring ());}}  16  .  //  Select the checkedlistbox1 option in the reverse direction.  For ( Int I = 0 ; I <checkedlistbox1.items. Count; I ++ ){  If  (Checkedlistbox1.getitemchecked (I) {checkedlistbox1.setitemchecked (I,  False );}  Else  {Checkedlistbox1.setitemchecked (I,  True  );}}  17  .  //  Checkedlistbox1-> checkedlistbox2  For ( Int I = 0 ; I <checkedlistbox1.checkeditems. Count; I ++ ) {Checkedlistbox2.items. Add (  This . Checkedlistbox1.checkeditems );  //  Remove removes a specific value, not an index. Note that       This  . Checkedlistbox1.items. Remove (  This . Checkedlistbox1.checkeditems );

 

 

 

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.