Get selected items in a Checkbox List in c#

 
Following example shows how to get selected items from checkedlistbox in c#.

In this example we will bind all checked items of checklistbox into listbox.

Get checked items in checkedlistbox
Get checked items in checkedlistbox

This example requires a windows form that contains checklistbox named CheckedlistBox1 and one listbox control named ListBox1 and button name Button1.

first we need to add some items in CheckListBox1:

private void Form1_Load(System.Object sender, System.EventArgs e)
{
	BindCheckedList();
}
 
private void BindCheckedList()
{
	CheckedListBox1.Items.Add("Author");
	CheckedListBox1.Items.Add("John");
	CheckedListBox1.Items.Add("Mohan");
	CheckedListBox1.Items.Add("James");
	CheckedListBox1.Items.Add("Ankur");
	CheckedListBox1.Items.Add("Robert");
}

The following code of lines get the selected items of CheckedListBox1 and bind into ListBox1.

private void Button1_Click(System.Object sender, System.EventArgs e)
{
	ListBox1.Items.Clear();
	if (CheckedListBox1.CheckedItems.Count > 0) {
		for (int i = 0; i <= CheckedListBox1.CheckedItems.Count - 1; i++) {
			ListBox1.Items.Add(CheckedListBox1.CheckedItems(i));
		}
	}
}
  • jm

    it bind this System.Data.DataRowView in my listbox

    • Remove the Tostring() method.

      make changes in the line for adding new item as:

      listBox1.Items.Add(checkLis.CheckedItems[i]);

      -or–

      listBox1.Items.Add(checkLis.CheckedItems[i].Text);