ProgressBar resets at 60%

后端 未结 4 651
花落未央
花落未央 2021-01-13 23:09

I have this code:

for(int k = 0; k<11; k++)
{
    pBar.Maximum = 10;
    pBar.Value = k;
    if (pBar.Maximum == k)
        pBar.Value = 0;
}
4条回答
  •  清酒与你
    2021-01-13 23:52

    pBar.Maximum = 10;
    int count = 0;
    
    Timer timer = new Timer();
    timer.Interval = 1000;
    timer.Tick += (source, e) =>
    {
        pBar.Value = count;
        if (pBar.Maximum == count)
        {
            pBar.Value = 0;
            timer.Stop();
        }
        count++;
    }
    timer.Start();
    

    Your problem is that you're using a loop. You need to use a timer so that the program has time to both perform the checks/assignments and update the screen.

    The code replaces the for loop with a timer that calls the body of the loop. Since a timer does not have an index variable, it is initialized outside of the timer (count) and updated with each tick.

提交回复
热议问题