问题
How can I create an array containing the checked items in a checkedlistbox using foreach loop (or any other way)?
I can't know the number of items in the list.
回答1:
Assuming your using 3.5 or above..
object[] items = lb.CheckedItems.OfType<object>().ToArray();
And if you are adding a specific type of object to the CheckedListBox then you can replace object with the name of the class you use.
回答2:
Hi i am doing a similar kind of task . But instead of array i am using array list . I used the below code
ArrayList errorList = new ArrayList();
errorList = chklbErrorlist.CheckedItems.OfType<object>().ToList();
Cannot implicitly convert type
System.Collections.Generic.List<object>
toSystem.Collections.ArrayList
I added the items to the array and then added to the arraylist, It worked. How to add items directly to the araaylist instead of the array
来源:https://stackoverflow.com/questions/3390122/creating-a-string-array-of-checked-items-in-checked-list-box