Creating a string-array of checked items in checked-list-box
Asked Answered
S

2

6

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.

Synod answered 2/8, 2010 at 17:15 Comment(1)
System.Windows.Forms.CheckedListBox.CheckedItems?Leandroleaning
N
13

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.

Nathalia answered 2/8, 2010 at 17:46 Comment(3)
Can you help me in one more thing please? I used what you wrote: string[] fonts = fontBox.CheckedItems.OfType<string>().ToArray(); and than i wanted to write each string in the array on a new line in the rich text box(called fontBox), so i tried: for (int i = 0; i < fonts.Count(); i++) { fontBox.Text = fonts[i]; } and i also tried: foreach (string i in fonts) { fontBox.Text = i; } but none of them workedSynod
for (int i = 0; i < fonts.Length; i++)Nathalia
Thank you very much, it's working =], and i located another mistake, i told the program to write in the checkedlistbox, not in the rich text box.Synod
D
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> to System.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

Dorrisdorry answered 8/2, 2012 at 10:25 Comment(1)
ArrayList errorList = new ArrayList(chklbErrorlist.CheckedItems.OfType().ToList());Synod

© 2022 - 2024 — McMap. All rights reserved.