Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Earlier today I asked how to pass listbox items to textbox and Mohamed Mitwalli solved it (THANKS AGAIN) but now I have question how to pass that same value back to listbox

in that earlier problem I had listbox items 1 and 2 and that was passed into textbox like this 1,2 but now I want to pass that back to listbox so that in listbox I have two items 1 and 2
Posted
Comments
Christian Amado 27-Aug-12 13:22pm    
Somo code?
shonezi 27-Aug-12 13:38pm    
I have no code

C#
string[] values = textBox.Text.Spilit(',');

foreach (string value in values)
{
    if(value.Trim()=="")
      continue;
    listBox.Items.Add(value.Trim());

}


this will help you.
 
Share this answer
 
Comments
shonezi 27-Aug-12 14:13pm    
THank you very much, very helpfull, thank you again
[no name] 27-Aug-12 14:16pm    
you are welcome.
[no name] 27-Aug-12 14:15pm    
+5Nice job.
[no name] 27-Aug-12 14:16pm    
Thanks meysam.
shonezi 27-Aug-12 14:16pm    
Thank you both :)))
try this one:

C#
string[] values = textBox.Text.Spilit(',');

foreach (string value in values)
{
    listBox.Items.Add(value.Trim());
}
 
Share this answer
 
v3
Comments
shonezi 27-Aug-12 13:42pm    
this code is for passing listbox to textbox but I need textbox to listbox but the problem is I have in textbox this ,1,2, and I want in listbox to add 1 and under it in second row of listbox 2
[no name] 27-Aug-12 13:46pm    
check new version!
shonezi 27-Aug-12 13:53pm    
this works, just one thing, I have empty spaces between rows in listbox like this

empty
1
empty
2
empty

while it should be
1
2
[no name] 27-Aug-12 13:55pm    
Trim() will remove the white spaces.
shonezi 27-Aug-12 14:03pm    
still the same
If you are interested in a WPF solution using MVVM: Dynamically Bound Dictionary to Windows Grid[^]
 
Share this answer
 
Comments
shonezi 31-Aug-12 13:55pm    
this is great, I am currently trying to learn WPF but really am just a begginer. thank you

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