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
2010-08-02 17:46:25
It's working, thank you very mu(n)ch =]
Gilad Naaman
2010-08-02 18:03:32
you are very welcome.
gbogumil
2010-08-02 18:09:01
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
2010-08-02 18:22:34
for (int i = 0; i < fonts.Length; i++)
gbogumil
2010-08-02 18:37:44
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
2010-08-02 19:01:28