C# Windows Form Application - value not printing in textbox for each time

1k views Asked by At

Value not printing in textbox for each time.. Iam able to see last value only (10) in textbox.

  private void button1_Click(object sender, EventArgs e)
    {
      for(int i = 0; i<=10; i++)
       {
         textBox1.Text = i.ToString();
         Thread.Sleep(100);
       }
    }
2

There are 2 answers

1
sangram parmar On BEST ANSWER

In C# Window application, control values are render after event is executed.

After your click event textbox is displaying last value that is updated.

If you want to render text-box value during event execution.You have to call refresh method of text-box to render value.

Use this.. You have to refresh textbox control.

 for (int i = 0; i <= 10; i++)
 {
    textBox1.Text = i.ToString();
    textBox1.Refresh();
    Thread.Sleep(100);
 }
5
Ashley McVeigh On

each time your code runs it sets all the text in the textbox to i, you need to use:

textBox1.Text += i.ToString();

instead of

textBox1.Text = i.ToString();