C # checkedlistbox control usage Summary (how to obtain multiple values)

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 following Code : For ( Int I = 0 ; I <checkedlistbox1.items. Count; I ++)
{
If (Checkedlistbox1.getitemchecked (I ))
{
MessageBox. Show (checkedlistbox1.getitemtext (checkedlistbox1.items [I]);
}
}

Reference: Recently I used the checklistbox control. It took a lot of time to use it. Here I collected its related code segments, hoping to help you.
1 .
Add item
Checkedlistbox1.items. Add ( " Blue " );
Checkedlistbox1.items. Add ( " Red " );
Checkedlistbox1.items. Add ( " Yellow " );

2 .
Checks whether the I-th item is selected. If this parameter is selected, the value is true. Otherwise, the value is false.
If (Checkedlistbox1.getitemchecked (I ))
{
Return True ;
}
Else
{
Return False ;
}

3 .
Set whether item I is selected
Checkedlistbox1.setitemchecked (I, True ); // If it is set to true or false, it is not selected.

4 .
Set select all
Add a checkbox control named select_all to control whether or not to select all checkedlistboxes.
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 radio 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 );
}

from: http://dotnet.5d6d.com/thread-486-1-1.html

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.