Search code examples
c#additioncheckedcheckedlistbox

How to select and list multiple items in a checkedlistbox C#


I am wondering how I can fix an error I am experiencing when I try to create a checkedlistbox, load a list into it, then attempt to display all the checked items in another listbox. For example, checkedlistbox1 displays ABC AAC ABB and I checkmark ABC and AAC, when I push a button, I want it to add ABC and AAC to listbox1 but all it gives me is "(collection)"

var selected = checkedListBox1.SelectedItems;
listBox1.Items.Add(selected);

Solution

  • You have to iterate the CheckedItems and add each item one-by-one:

        private void button1_Click(object sender, EventArgs e) {
            listBox1.Items.Clear();
            foreach (var item in checkedListBox1.CheckedItems) {
                listBox1.Items.Add(item);
            }
        }