It is generally believed that foreach (object obj in ) can traverse the selected value.
In fact, what is traversed here is only the highlighted value, not the ticked value. To traverse the value of the checked chew, use the following code:
for (int i = 0; i < ; i++) { if ((i)) { (([i])); } }
I have recently used the checklistbox control and spent a lot of time using it. Here I have collected related code snippets, hoping it will be helpful to everyone.
1. Add items
(“blue“); (“red“); (“yellow“);
2. Determine whether the i-th item is selected, select it as true, otherwise it is false
if((i)) { return true; } else { return false; }
3. Set whether item i is selected
(i, true); // Change true to false to not selected.
4. Set Select All
Add a checkbox control named select_all to control whether checkedListBox is selected or not.
private void select_all_CheckedChanged(object sender, EventArgs e) { if(select_all.Checked) { for (int j = 0; j < ; j++) (j, true); } else { for (int j =0; j < ; j++) (j, false); } }
5. Get all selected values and combine the text of the selected item into a string.
string strCollected = ; for (int i = 0; i < ; i++) { if ((i)) { if (strCollected == ) { strCollected = ( [i]); } else { strCollected = strCollected + “/“ + checkedListBox1. GetItemText([i]); } } }
6. Set the Checked state of item i in CheckedListBox
(i, );
7.
private void checkBoxAll_CheckedChanged(object sender, EventArgs e) { if () { // If selected, all entries in CheckedListBox will be checked to the Checked state for (int i = 0; i < ; i++) { (i, ); } } else { //Otherwise it becomes Unchecked for (int i = 0; i < ; i++) { (i, ); } } }
8. CheckedListBox Single-choice setting (code implementation)
private void chkl_ItemAuditing_ItemCheck(object sender, ItemCheckEventArgs e) { if (chkl_ItemAuditing. > 0) { for (int i = 0; i < chkl_ItemAuditing.; i++) { if (i != ) { this.chkl_ItemAuditing.SetItemCheckState(i, ); } } } }
9. checkedListBox1 displays all records corresponding to keywords in a database
for (int i = 0; i < ; i++) { string name = ["myname"].ToString(); string paw = ["mypaw"].ToString(); (name + paw); }
10.
for(i=0;i<;i++) { if(( )==“The value you get“) { (i,true); } }
11. Clear all options in checkedListBox1
for (int i = 0; i < ; i++) { (); }
12.
//Set the index to index to the selected statefor (int i = 0; i < ; i++) { (i, true); }
13.
for (int i = 0; i < ; i++) { if ((i)) { (()); } }
14.
//Select all options of checkedListBox1 for (int i = 0; i < ; i++) { (i, ); }
15.
for (int i = 0; i < ; i++) { //If the i-th item of checkedListBox1 is selected, //The value corresponding to checkedListBox1 is displayed if ((i)) { (( )); } }
16.
//Reversely select the option to checkedListBox1for (int i = 0; i < ; i++) { if ((i)) { (i, false); } else { (i, true); } }
17.
//The selected item in checkedListBox1->checkedListBox2for (int i = 0; i < ; i++) { (this.); //Remove is to remove a specific value, not index, please pay attention this.( this.);
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.