views:

44

answers:

1

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. C#

+1  A: 

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.

gbogumil
It's working, thank you very mu(n)ch =]
Gilad Naaman
you are very welcome.
gbogumil
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 worked
Gilad Naaman
for (int i = 0; i < fonts.Length; i++)
gbogumil
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.
Gilad Naaman