Creating a string-array of checked items in checked-list-box

Gilad Naaman picture Gilad Naaman · Aug 2, 2010 · Viewed 12.4k times · Source

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.

Answer

Greg Bogumil picture Greg Bogumil · Aug 2, 2010

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.