0

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
NickHallick
  • 239
  • 1
  • 7
  • 24
  • Where is the `number` variable created/set? As far as your code there, it seems that `number`, your labels, and the progress bar never change unless the the value is `100` or `0`? Is that on purpose? Can you clarify what exactly your code is supposed to be doing? – Code Maverick Jul 14 '14 at 20:03
  • Sounds related to my answer here: http://stackoverflow.com/a/23682919/1842065 – Bjørn-Roger Kringsjå Jul 14 '14 at 20:14
  • This is normal, ProgressBar animates progress. Making it smoothly increase, even if you force it to step. The animation inevitably causes lag. You are moving it too fast. A silly trick is to force it to overshoot and then move back. – Hans Passant Jul 14 '14 at 20:20
  • @HansPassant - Yea, I agree, that's what I was suggesting in my answer. – Code Maverick Jul 14 '14 at 20:22
  • @Codemaverick `number` is constantly changing and is represented in label2 when incrementing until it reaches 100 then it decrements and is represented in label3 until it is 0. the progressbar is just mirroring the value of `number`. – NickHallick Jul 15 '14 at 12:10

1 Answers1

1

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.

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
  • i mean bumping up the interval of the timer works fine but it also takes a while for the bar to fill up and then go back down. i tried your code and it looks the same as it did before unless i change the timer interval. Im thinking there may be no way to get around the animation lag. – NickHallick Jul 15 '14 at 12:08
  • @NickHallick - Well, there is a *hacky* way to get around it as mentioned in [this SO answer](http://stackoverflow.com/a/5332770/682480). As far as the time it takes to fill up and go back down, you did say you were just "messing" around, so you could just mess with the interval to speed things up or you could increment/decrement the `currentValue` by more than just `1`, say `10`, for example. – Code Maverick Jul 15 '14 at 13:23
  • Yeah i am in no way relying on any definite answer i am just messing around. I did play around with it in vb6 as well and it works perfectly there but i can live with it in vb 2008 for a useless program! thanks for the insight! – NickHallick Jul 15 '14 at 14:10