Copy items from CheckedListBox to ListBox

2019-08-25 05:33发布

I'm trying to copy the CheckedItems from a CheckedListBox to a Listbox, but I am not getting it right.

I have tried

Listbox.Items.Add(checkedlistbox.CheckedItems);

but that only gives me a (collection)

Does anyone have a great line of code to share? :D

2条回答
【Aperson】
2楼-- · 2019-08-25 05:59

This should work:

foreach(var Item in checkedlistbox.CheckedItems)
    Listbox.Items.Add(Item);

Edit: replaced string with var so it works with non-string types too.

查看更多
beautiful°
3楼-- · 2019-08-25 06:17
        string item = checkedListBox1.SelectedItem.ToString();
if (e.NewValue == CheckState.Checked)
    listBox1.Items.Add(item);
else
    listBox1.Items.Remove(item);

You should write it in the ItemCheck event. With this code you can show the checked items in the other listBox.

查看更多
登录 后发表回答