Click here to Skip to main content
15,923,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi..

i want to add selected multiple items form list box to text box(MultiLine) seperated with a comma in each item.I know how to add multiple list box values to another List box. but how can i add for the text box.

it will be a grate help if some one can help me with this.
thank you.
Posted

In addition to what the other responder said, I'd use a StringBuilder to combine the strings. It's generally a good idea to use a StringBuilder when combining a significant number of strings together. Also, instead of using the default inequality operator, I'd use object.ReferenceEquals to compare the two objects.

Also, I know you didn't ask for a newline to be inserted after each comma, so you can take that out of the code the responder gave you. But if you do want to insert a newline, I'd use Environment.NewLine rather than "\r\n".

Once you are done creating the StringBuilder, call ToString and assign the result to the Text property of the TextBox you are using.
 
Share this answer
 
If you know how to process multiple lines out of a list box, simply append the values together either into a string, then assign the value of the string to the text box, or directly into the text box.
 
Share this answer
 
v2
Well, my solution for that would be :

consider we have a listbox on form, with its 'SelectionMode' property set to value 'MultiSimple', and a textbox which 'Multiline' property is true, and 'WordWrap' property is false. and of course a button to add items from listbox to textbox.

my code in the 'button1_Click' event would be :

private void button1_Click(object sender, EventArgs e)
{
int count = listBox1.SelectedItems.Count;

foreach (object item in listBox1.SelectedItems)
{
textBox1.Text += item.ToString();

if (listBox1.SelectedItems[count - 1] != item)
textBox1.Text += ", \r\n";
}
}

So, each selected item in listbox will be added ( as an string via ToString() function) to textbox's text, and if it isnt the last selected item, a comma and a new line will be added.

------------------------
Regards

H.Maadani
 
Share this answer
 
Comments
ganiganu 16-Jul-13 3:49am    
not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900