开发者

Disabling .NET progressbar animation when changing value?

I realize there are other questions on SO regarding animations and progressbars, but they seem to revolve around getting rid of the animation drawn on top of the progress bar, ie. the highlight that travels over it.

What I want to do is to get rid of the animation that is used when I set the new value of the progress bar. The problem I have now is that the action that is running completes and then the progress bar continues to increase up to their max position after the action has completed.

In other words, if I set the Value property of the progressbar to 50, I want it to travel to the halfway position (if max is 100) immediately, not slowly build up the progressbar to that position as it does now.

If there is indeed a question on SO that already deals with this, just close as duplicate and I'll happily delete it, but I could not find any.

This is the one I found: Disabling WinForms ProgressBar animation, and it deals with the highlight that is animated, and that's not what I'm talking about.

Here's a simple LINQPad demo that shows the problem:

void Main()
{
    using (var fm = new Form())
    {
        var bt = new Button
        {
            Text = "Start",
            Location = new Point(8, 8),
            Parent = fm,
        };
        var pb = new ProgressBar
        {
            Top = bt.Top + bt.Height + 8,
            Wi开发者_C百科dth = fm.ClientRectangle.Width - 16,
            Left = 8,
            Parent = fm
        };

        bt.Click += (s, e) =>
        {
            bt.Enabled = false;
            Thread t = new Thread(new ThreadStart(() =>
            {
                Thread.Sleep(1000);
                bt.BeginInvoke(new Action(() => { pb.Value = 50; }));
                Thread.Sleep(1000);
                bt.BeginInvoke(new Action(() => { pb.Value = 100; }));
                bt.BeginInvoke(new Action(() => { bt.Enabled = true; }));
            }));
            t.Start();
        };
        fm.ShowDialog();
    }
}

Edit 1: This is Windows 7, Glass theme, so yes, I bet this is specific to 7 or possibly also Vista.

Here's a GIF-animation that shows the problem, the project from above. You can see that as soon as the button becomes enabled, 1 second after the halfway mark has been set, the progressbar animates up to 100%, after the button has become enabled.

As you can see above, setting the button back to enabled and setting the progressbar to 100 is done "at the same time". Basically, I don't want the progressive buildup of the progressbar, I want it to jump directly to 50% and then to 100% at the same time as the button becomes enabled.

Disabling .NET progressbar animation when changing value?


Edit 2: In response to David Heffernan's answer, this is how I changed the above code:

bt.BeginInvoke(new Action(() => { pb.Value = 51; pb.Value = 50; }));
Thread.Sleep(1000);
bt.BeginInvoke(new Action(() => { pb.Maximum = 101; pb.Value = 101;
                                  pb.Maximum = 100; pb.Value = 100; }));


This animation feature was introduced in Vista with the Aero theme.

There is a workaround though. If you move the progress backwards, the animation is not shown. So if you want it to advance by 50 instantly, increment Value by 51, then immediately decrement by 1.

You get into strife when close to 100% because you can't set Value to 101 (I'm assuming Maximum is set to 100). Instead set Maximum to 1000, say, increase to 1000, decrease to 999, and then move back to 1000.

Anyway, it's kind of weird, but it does have the benefit of giving you the desired effect!


Here is my extension method, based on David Heffernan's recommendation:

Wrap it up, hide it from view, and pretend it's not there!

public static class ExtensionMethods
{
    /// <summary>
    /// Sets the progress bar value, without using Windows Aero animation
    /// </summary>
    public static void SetProgressNoAnimation(this ProgressBar pb, int value)
    {
        // Don't redraw if nothing is changing.
        if (value == pb.Value)
            return;

        // To get around this animation, we need to move the progress bar backwards.
        if (value == pb.Maximum) {
            // Special case (can't set value > Maximum).
            pb.Value = value;           // Set the value
            pb.Value = value - 1;       // Move it backwards
        }
        else {
            pb.Value = value + 1;       // Move past
        }
        pb.Value = value;               // Move to correct value
    }
}


There is another way to skip the animation of a vista-style progress bar: Just SetState() the control to PBST_PAUSED, then set the value and finally set it back to PBST_NORMAL.


Now follows a VB.Net 2.0 and higher version of Jonathan Reinharts method SetProgressNoAnimation. I hope this will help other VB developers. The function setProgressBarValue is almost crashproof. Hardware failure can bring it down though.

''' <summary>
''' In VB.Net, the value of a progress bar can be set without animation.
''' Set the minimum and the maximum value beforehand.
''' This VB version has been written by EAHMK (Evert Kuijpers) in Tilburg in The Netherlands.
''' See SetProgressNoAnimation in
''' https://stackoverflow.com/questions/5332616/disabling-net-progressbar-animation-when-changing-value/5332770
''' by Jonathan Reinhart, based on the suggestion of David Heffernan.
''' </summary>
''' <param name="progressBar">
''' The progress bar that is to present the new value.
''' </param>
''' <param name="newValue">
''' The new value to present in the progress bar.
''' </param>
Public Function setProgressBarValue(progressBar As ProgressBar,
                                    newValue As Integer) As Exception
  Try
    ' Extremes are not supported.
    If newValue < progressBar.Minimum _
     Or newValue > progressBar.Maximum _
     Or progressBar.Maximum = progressBar.Minimum _
     Or progressBar.Maximum = Integer.MaxValue Then
      Return New ArgumentException("The value " & CStr(newValue) & " for" _
                & " the progress bar '" & progressBar.Name & "' is out of bounds.")
    End If

    ' By field maximumReached also progress bar value progressBar.Maximum is supported.
    Dim maximumReached As Boolean = newValue = progressBar.Maximum
    If maximumReached Then
      progressBar.Maximum += 1
    End If
    progressBar.Value = newValue + 1

    progressBar.Value = newValue
    If maximumReached Then
      progressBar.Maximum -= 1
    End If
    ' The value has been presented succesfully in the progress bar progressBar.
    Return Nothing
  Catch ex As Exception
    ' Returns an exception but does not crash on it.
    Return ex
  End Try
End Function


My absolute solution for this problem in VB...

Sub FileSaving()

    With barProgress
        .Minimum = 0
        .Maximum = 100000000
        .Value = 100000
    End With

    For
        ...
        saving_codes
        ...
        With barProgress
            .Maximum = .Value * (TotalFilesCount / SavedFilesCount)
        End With
    Next

End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜