Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have a loop in wpf that i want to write it, it seems simple but new to wpf so dont know exact way to do it.
I have
C#
for (int 1=0; i<10; i++)
{
textBlock1.Text="Number is:" +i.ToString();
}

so the output should be
Number is: 1
Number is: 2
Number is: 3
.......
Number is: 9

but the above textBlock doesnt work, any suggestions how it can be implemented?
Thanks
Posted
Updated 31-May-12 9:46am
v2

1 solution

Change the "=" to a "+=" is the simplest to implement:
C#
for (int 1=0; i<10; i++)
   {
   textBlock1.Text="Number is:" +i.ToString();
   }
Becomes
C#
for (int 1=0; i<10; i++)
   {
   textBlock1.Text+="Number is:" +i.ToString();
   }
But a better solution would be to use a StringBuilder:
C#
StringBuilder sb = new StringBuilder();
for (int 1=0; i<10; i++)
   {
   sb.AppendFormat("Number is: {0}\n", i);
   }
textBlock1.text = sb.ToString();
 
Share this answer
 
Comments
db7uk 31-May-12 14:52pm    
Yes My 5! Often I see string concatenation using += etc. StringBuilder (IMHO) should always be considered if multiple strings are being created!
Oleksandr Kulchytskyi 31-May-12 15:51pm    
It's great that you mentioned about string concatenations.
Often it became a big punch for beginners, while they operate with string data.
Maciej Los 31-May-12 16:00pm    
Good advise, my 5!

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