Winform numericUpDown have different delay for up and down value change

260 Views Asked by At

I have just started studying .Net Winform with C# (.Net 5). I did a simple winform with a NumericUpDown and a ProgressBar, basically, the ProgressBar will update its value when the NumericUpDown's value changes via the ValueChanged event.

Here is my code:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
   progressBar2.Value = (int)numericUpDown1.Value;
}

I notice that the ProgressBar update took about half a second to detect the NumericUpDown ValueChanged when the value is increasing (from 0 to 1, 1 to 2, 2 to 3, etc.) no matter whether I was holding the Up key to increase it continuously or press one at a time.

However, when the value is decreasing, the progress bar update instantly, even when I hold down the Down button to decrease it continuously. Which is pretty weird.

Even when I enter the value directly, it still does the same thing: enter 80 when it is at 20 took a bit to fill up while enter 20 when it is 80 update instantly

The most obvious way to see it is holding the Up button to continuously increasing the value and then press the Down key once. The Progress Bar fill up slowly and then suddenly jump to the correct value when the Down key is pressed

My question is: Is this the default behavior? Does Visual Studio had different algorithm to detect increase or decrease value change? Or am I missing something?

1

There are 1 best solutions below

5
JohnG On

I am pretty sure what you are seeing is the default behavior of the ProgressBar Control. When the progress bar increases, there is a small “delay” to show each step. I may be mistaken; however, I could not find any particular property in the ProgressBar control that would remove or shorten this “delay.”

Fortunately, I did find a fairly simple solution that appears to work. In this solution the idea is to “increase” the current value from the NUD by one (1). Then set the progress bars value to that value, then immediately change the progress bars value back to the true value.

The only issue is if the NUDS value is at the maximum of the progress bar. If we try and increase the NUDS value by 1, then we would go out of bounds on the progress bar. So, in that case we simply need to increase the progress bars maximum value by 1, set the value, then immediately set it back to the original maximum value.

See if the code below works as you are wanting. Please let me know as I will remove it if it does not work as you are wanting.

private void numericUpDown1_ValueChanged(object sender, EventArgs e) {
  //progressBar1.Value = (int)numericUpDown1.Value;
  if ((int)numericUpDown1.Value == progressBar1.Maximum) {
    progressBar1.Maximum = progressBar1.Maximum + 1;
    progressBar1.Value = (int)numericUpDown1.Value + 1;
    progressBar1.Value = (int)numericUpDown1.Value;
    progressBar1.Maximum = progressBar1.Maximum - 1;
  }
  else {
    progressBar1.Value = (int)numericUpDown1.Value + 1;
    progressBar1.Value = (int)numericUpDown1.Value;
  }
}