Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
foreach (string text in storedata)
        {
          txtclients.Text = txtclients.Text + text + "\n";
        }


Txtclients is id for multiline Text Box
Could you please share the logic as dont want "\n" to be execute when foreach loop is executing last time.

What I have tried:

Could you please share the logic as dont want "\n" to be execute when foreach loop is executing last time.
Posted
Updated 15-Aug-16 5:40am

If you're replacing the entire contents of the textbox, try the simple approach:
C#
txtclients.Text = string.Join("\n", storedata);
 
Share this answer
 
Comments
niceaashish 15-Aug-16 13:00pm    
Thanks A lot, it worked.
After the loop you could add

txtclients.Text = txtclients.Text.TrimEnd('\n');
 
Share this answer
 
Comments
niceaashish 15-Aug-16 5:25am    
It is still showing space in the last line ...
I have tried outside loop and inside loop as well
niceaashish 15-Aug-16 5:32am    
Actually this trim function is trimming that \n but the next is created , how to get rid of it
F-ES Sitecore 15-Aug-16 6:03am    
Get the count of items in the list, and create a counter variable that you increase in the loop so you always know which item you are processing, and put an if around the concatenation so it only adds if it's not the final item in the list, eg if (counter < storedata.Count).

Or if it's possible change your "foreach" into a "for" loop so you key the row count that way, and again put the "if" around the line you don't want to execute.
First, choose the language:
- the code is not basic
The principle of foreach is avoid having a counter thus, in the loop, you don't know when you are in last iteration.
The traditional solution is more like:
C#
txtclients.Text = txtclients.Text + storedata[0];
for (int i=1; i< storedata.count; i++)
{
    txtclients.Text = txtclients.Text + "\n" + storedata[i];
}
 
Share this answer
 
Comments
niceaashish 15-Aug-16 7:08am    
In this Case i will get a empty line on the top instead in the end
because first it will create a line and then will store the data
Patrice T 15-Aug-16 7:12am    
No, look better at my code.
first line matters too.

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