Reputation: 6490
I want to display all elements from a ListBox to a TextBox. I'm not sure how to do this, I have tried doing foreach statement but it doesn't work for the reason that ListBox doesn't contain IEnumerator.
How to do this?
Upvotes: 1
Views: 28071
Reputation: 25619
foreach (ListItem liItem in listBox1.Items)
textBox1.Text += liItem.Value + " "; // or .Text
EDIT:
Since you're using WinForms, ListBox.Items
returns an ObjectCollection
foreach (object liItem in listBox1.Items)
textBox1.Text += liItem.ToString() + " "; // or .Text
Upvotes: 2
Reputation: 8502
The Items collection of Winforms Listbox returns a Collection type of Object so you can use ToString()
on each item to print its text value as below:
string text = "";
foreach(var item in yourListBox.Items)
{
text += item.ToString() + "/n"; // /n to print each item on new line or you omit /n to print text on same line
}
yourTextBox.Text = text;
Upvotes: 6
Reputation: 3091
Try running your foreach on Listbox.Items..that has an enumerator that you can use
Upvotes: 1