NickHallick
NickHallick

Reputation: 239

Progressbar lag?

I am just messing around in vb express 2008 and i decided to play with the progressbar control. I set up two labels and a timer to make the progressbar value go up then go down when it reaches its limit. it then repeats this. It works great except for the fact that the progressbar is slow to respond to increment and it jumps to the maximum value near the end of its increment cycle. I have the timer set to a 1 millisecond interval and this is my code,

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

    If Label2.Text = "100" Then
        number = number - 1
        Label3.Text = number
        ProgressBar1.Value = number
        ProgressBar1.Update()
    End If
    If Label3.Text = "0" Then
        number = number + 1
        Label2.Text = number
        ProgressBar1.Value = number
        ProgressBar1.Update()
    End If
End Sub

Upvotes: 0

Views: 1594

Answers (1)

Code Maverick
Code Maverick

Reputation: 20415

First of all, I would set the interval to something like 500, then step it back to 400, 300, 200, 100, etc. until you get the desired speed you want.

Secondly, assuming Label2 is your max value and Label3 is your min value and that you want your progress bar control to simply bounce back and forth between the min and max values, I would do something like this:

Const MAX_VALUE = 100
Const MIN_VALUE = 0

Dim currentValue = 0
Dim isIncrementing = True

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

    If currentValue = MAX_VALUE Then isIncrementing = False
    If currentValue = MIN_VALUE Then isIncrementing = True

    If isIncrementing Then currentValue += 1 Else currentValue -= 1

    ProgressBar1.Value = currentValue
    ProgressBar1.Update()

End Sub

Since I'm assuming your Label2 and Label3 are your min/max values, I'm also assuming they should stay static and never change, which is why I don't change their values in the Tick event. If you wanted a Current Value label, that would be easy enough to add and that would be changed at the same time the ProgressBar1.Value is.

Upvotes: 1

Related Questions