Reputation: 2130
I'd like to be able to collect all the string values in a Winform ListBox
. At the moment my code loops through the ListBox
and gets the values, but it's appending all values together in one long string:
private string GetFormNumberValues()
{
string formNumbers = "";
foreach (string item in this.lbFormNumbers.Items)
{
formNumbers += item.ToString();
}
return formNumbers;
}
How can I collect each individual string value to use for later? Thanks.
Upvotes: 1
Views: 236
Reputation: 125217
You can have them in a List this way:
var list = listBox1.Items.Cast<object>().Select(x => x.ToString()).ToList();
Upvotes: 4
Reputation: 8551
Try something like this:
private string[] GetFormNumberValues()
{
List<string> strings = new List<string>();
foreach (string item in this.lbFormNumbers.Items)
{
strings.Add(item.ToString());
}
return strings.ToArray();
}
(Depending on your needs, you could simplify this by returning a List
rather than an array...)
Upvotes: 3