tags:

views:

391

answers:

3

I have listbox, button, and textbox controls in a Windows application. How can I display multiple selected values in a textbox.

this is my code

textBox1.Text = listBox1.SelectedItems.ToString();

but it display in textbox like this: (I select more than one item)

System.Windows.Forms.ListBox+Selec.

please help me

+1  A: 

You need to iterate over the collection of items. Something like:

textBox1.Text = "";
foreach (object o in listBox1.SelectedItems)
   textBox1.Text += (textBox1.Text == "" ? "" :", ") + o.ToString();
JDunkerley
oh thank u so thank ful
Surya sasidhar
+2  A: 

You could do something like:

 string text = "";
foreach(var item in listBox1.SelectedItems) {

     text += item.ToString() + " ";
  }
 textBox1.Text = text;
Robban
oh ya it is working so thanks to u Robban
Surya sasidhar
If there are many many elements, think about using StringBuilder as it's more efficient...
jdehaan
+1  A: 

ListBox.SelectedItems: Returns a collection of the currently selected items.

Loop through the SelectedItems collection of the listbox.

foreach (ListItem liItem in ListBox1.SelectedItems)
{
    // write your code.   
}
rahul
ya it is working so thank phoenix
Surya sasidhar