I want my form to display 1,2,3,4,5 but all it does is replace the text again and again.
for (int i = 1; i <= 5; i++)
{
richTextBox1.Text = Convert.ToString(i);
}
I know it's because of the .Text that it always overrides itself. But how can i leave them in the form so it will display:
1
2
3
4
5
The problem is that in your loop, you're completely replacing the text with each iteration. So the text is left with whatever the last value of i was.
Try putting adding to the current text (with +=) and putting a new line (Environment.NewLine or "\n") between each number:
for (int i = 1; i <= 5; i++)
{
richTextBox1.Text += Environment.NewLine + Convert.ToString(i);
}
Or alternatively, a little Linq can make your life a lot easier:
richTextBox1.Text = string.Join(Environment.NewLine, Enumerable.Range(1, 5));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With