Copy items from CheckedListBox to ListBox

谁说我不能喝 提交于 2019-12-13 04:29:07

问题


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


回答1:


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.




回答2:


        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.



来源:https://stackoverflow.com/questions/11917749/copy-items-from-checkedlistbox-to-listbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!