Disabling .NET progressbar animation when changing value?

后端 未结 5 1316
灰色年华
灰色年华 2020-11-27 05:56

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 ba

相关标签:
5条回答
  • 2020-11-27 05:57

    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
        }
    }
    
    0 讨论(0)
  • 2020-11-27 05:57

    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
    
    0 讨论(0)
  • 2020-11-27 05:59

    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 讨论(0)
  • 2020-11-27 06:00

    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.

    0 讨论(0)
  • 2020-11-27 06:19

    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!

    0 讨论(0)
提交回复
热议问题