I'm using Windows Vista and Visual Studio 2010. Create a .Net 4 Windows Forms Application. Drop a progress bar on the default form, add code to handle the form load event and do a progressBar1.Value = 100;
there.
Start debugging and you see an animation moving the progress bar to 100 in about half a second.
I need 2 progress bars in my project. One is for "global progress" and the second is for "current step progress" so the second goes from 0 to 100 and hen back to 0 for the next step. The problem is that with the progress bar being slow for some of the quick steps it never reaches 100 and it looks weird.
Is there a way to get rid of that animation? In WPF it's OK but I'd rather stay with Windows Forms.
You can update the percentage of progress displayed by using the setProgress(int) method, or by calling incrementProgressBy(int) to increase the current progress completed by a specified amount. By default, the progress bar is full when the progress value reaches 100.
A ProgressBar control is used to represent the progress of a lengthy operation that takes time where a user must wait for the operation to be finished. In this article, we will see how to create a ProgressBar control in a Windows Forms application using Visual Studio 2017.
This is just how the Vista/7 progress bar is designed. When you change the value of the progress bar, the bar is animated to that value progressively.
The only way I know of avoiding this problem is to go backwards when updating the progress bar, as follows:
progressBar1.Value = n;
if (n>0)
progressBar1.Value = n-1;
For a more complete discussion see Disabling .NET progressbar animation when changing value?
Building off of Heffernan's tip on going backwards with the progress bar and Reinhart's extension method approach in a related question, I came up with my own solution.
The solution is pretty seamless and successfully handles the issue you will encounter when the value is at Maximum
. This extension method to ProgressBar
alleviates the lagging that is caused from the progressive animation style present in the WinForms ProgressBar
control when running on Windows Vista and 7 (I haven't tested on Windows 8 yet).
public static class ExtensionMethods
{
/// <summary>
/// Sets the progress bar value, without using 'Windows Aero' animation.
/// This is to work around a known WinForms issue where the progress bar
/// is slow to update.
/// </summary>
public static void SetProgressNoAnimation(this ProgressBar pb, int value)
{
// To get around the progressive animation, we need to move the
// progress bar backwards.
if (value == pb.Maximum)
{
// Special case as value can't be set greater than Maximum.
pb.Maximum = value + 1; // Temporarily Increase Maximum
pb.Value = value + 1; // Move past
pb.Maximum = value; // Reset maximum
}
else
{
pb.Value = value + 1; // Move past
}
pb.Value = value; // Move to correct value
}
}
Sample usage:
private void backgroundWorker_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
progressBar.SetProgressNoAnimation(e.ProgressPercentage);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With