llk
llk

Reputation: 2561

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);

Upvotes: 0

Views: 3825

Answers (1)

Hans Passant
Hans Passant

Reputation: 941545

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);
        }
    }

Upvotes: 1

Related Questions