Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert ListBox.items to string of array collection in c#

I have bound array as datasource for ListBox . Now i need to convert listbox.Items to string of array collection.

foreach (string s1 in listBoxPart.Items)
{
   clist.Add(s1);
}

here clist is string list, so how can i add ListBox.items to clist?

like image 834
user3631120 Avatar asked Jun 11 '15 07:06

user3631120


2 Answers

You can project any string contained inside Items using OfType. This means that any element inside the ObjectCollection which is actually a string, would be selected:

string[] clist = listBoxPart.Items.OfType<string>().ToArray();
like image 167
Yuval Itzchakov Avatar answered Nov 03 '22 12:11

Yuval Itzchakov


for (int a = 0; a < listBoxPart.Items.Count; a++)
    clist.Add(listBoxPart.Items[a].ToString());

This should work if the items saved in the list are actualy strings, if they are objects you need to cast them and then use whatever you need to get strings out of them

like image 3
Vajura Avatar answered Nov 03 '22 10:11

Vajura