A button has been added to the form, after which a certain cycle is executed.

private void button1_Click(object sender, EventArgs e){ for (int i = 1; i <= numVal; i++) 

At the end of the loop iteration, you need to change the value of the ProgressBar located on the same form, do this:

  progressBar1.Value = (i / numVal) * 100; 

Unfortunately, the ProgressBar value is not updated during the iterations of the loop, but becomes equal to 100 (the maximum value for it) after all iterations have been completed.

The same happens with TextBox located on the same form.

The execution time of all iterations of the loop is not so short that changes cannot be seen (each iteration is on average 7 seconds).

Tell me what needs to be changed in order for the ProgressBar and TextBox updates to occur during the execution of the cycle, and not after it has been completely worked out?

  • You have an integer division (i / numVal) because 100 appears only at the last iteration, and all the others are 0. Change to (double)i / numVal . - 伪位蔚蠂慰位蠀蟿
  • Another problem may be that you need to update the drawing on the form. The easiest way is to call Application.DoEvents(); after setting the new value of the progress bar. - 伪位蔚蠂慰位蠀蟿
  • @alexolut thanks, it was really in the types! I did not translate, just transferred the multiplication by 100 to the beginning of 'progressBar1.Value = 100 * i / numVal;' - iamx4nd3r
  • @alexolut and your second tip helped solve the problem of filling a textbox, one more thank you! :) - iamx4nd3r
  • If the answer solved the problem - please tick the box. - 伪位蔚蠂慰位蠀蟿

1 answer 1

You have an integer division (i / numVal) , because 100 appears only at the last iteration, and on all others it is 0 . Change to (double)i / numVal .

Another problem may be that you need to update the drawing on the form. The easiest way is to call Application.DoEvents(); after setting the new value of the progress bar.