Reputation: 194
I want to display something multiple times in a textbox. For example if you use this code and replace richtextbox with messagebox, it will keep displaying the text until the loop ends. I want to display the text from textBox1 into richTextBox1, and then have the program hit enter, and then type it out again in the richtextbox. It's kind of confusing sorry, but if you have any questions just comment them and i'll be more clear. This is my code:
private void button1_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox1.Text);
int text = 0;
int end = int.Parse(textBox2.Text);
while (text<=end)
{
richTextBox1.Text=(Clipboard.GetText());
text++;
}
Thanks in advance!
Upvotes: 0
Views: 1659
Reputation: 4739
In your code you have:
richTextBox1.Text=(Clipboard.GetText());
The reason that your code is not working is because in every loop, you are setting the text to whatever is on the clipboard, so at the end of the loop it will only have it in there once. You need to 'append' or add onto the end of the text so it will have it multiple times:
richTextBox1.Text += richTextBox1.Text + (Clipboard.GetText());
Or:
richTextBox1.Text += (Clipboard.GetText());
This will add the clipboard text onto the end of the RichTextBox, so you will have the same text multiple times, but all on the same line. If you want to make the text appear on multiple lines, you have to add a new line after appending the text:
richTextBox1.Text += (Clipboard.GetText())+"\r\n";
Or:
richTextBox1.Text += (Clipboard.GetText())+Enviroment.NewLine;
Hope this Helps!
Upvotes: 3
Reputation: 3211
Use Timer instead of using loop and keep its interval time like for 2 second. and on button click start timer and declare end as class variable , when condition is met for "end" variable , stop timer.
private void button1_Click(object sender, EventArgs e)
{
end = int.Parse( textBox2.Text);
timer1.Start();
}
private int end = 0;
private int start = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (start == end)
{
timer1.Stop();
}
else
{
start++;
textBox1.Text = start.ToString();
}
}
Upvotes: 0