dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] progressbar with decreasing values

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2015
    Location
    Connecticut, USA
    Posts
    139

    Resolved [RESOLVED] progressbar with decreasing values

    I know how to display a progressbar when the value is increasing - set min and max values, set value to the min number, and increment the value. But what if the value is decreasing?
    Example, an environmental test chamber currently at 100 degrees and set to 25 degrees. How do I advance the progress bar to show decreasing temperature?

    TIA

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: progressbar with decreasing values

    Assuming you still want it to go left-to-right, it's a matter of doing math.

    Normally, the progress bar is at "no value" when the value is Minimum, and at "full" when the value is Maximum. That is to say, it decides how much progress to show by considering the percentage defined by:
    Code:
    Let range = Maximum - Minimum
    Let percent = (Value - Minimum) / range
    Let's check that work. We know if minimum is 100 and Maximum is 200, then we should get 50% at 150:
    Code:
    Let range = 200 - 100 = 100
    Let percent = (150 - 100) / 100 = 50 / 100 = 0.5
    So long as you maintain that relationship, you will get the percentage you want.

    So to invert it is fairly simple. We want to end up with the OPPOSITE percentage for any value. 50% was a bad choice above. Let's consider the same scale. When moving from 100 to 200, 125 should be 25%. But if we're moving from 200 to 100, we want 175 to be 25%. That's actually easy to pull off:
    Code:
    Let range = Maximum - Minimum
    Let invertedValue = Maximum - Value
    Let percent = invertedValue / range
    To double check:
    Code:
    Let range = 200 - 100 = 100
    Let invertedValue = 200 - 175 = 25
    Let percent = 25 / 100 = 0.25
    Of course, we don't set the percent, we set a value. 'invertedValue' above isn't within the range. We can adjust it:
    Code:
    Let finalValue = Minimum + invertedValue = 100 + 25 = 125
    So instead of directly setting the progress, write a helper function like this:
    Code:
    Sub SetInvertedProgress(ByVal value As Integer)
        Dim range = pbProgress.Maximum - pbProgress.Minimum
        Dim invertedValue = pbProgress.Maximum - value
        pbProgress.Value = (pbProgress.Minimum + invertedValue) \ Range
    End Sub
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2015
    Location
    Connecticut, USA
    Posts
    139

    Re: progressbar with decreasing values

    Thanks Sitten, but it doesn't seem to work. For the case of :

    Code:
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = 100
            Dim value As Integer = 50
    
            SetInvertedProgress(value)
    I would expect a 50% graph.


    Code:
    Sub SetInvertedProgress(ByVal value As Integer)
            Dim range = ProgressBar1.Maximum - ProgressBar1.Minimum
            Dim invertedValue = ProgressBar1.Maximum - value
            ProgressBar1.Value = (ProgressBar1.Minimum + invertedValue) \ range
        End Sub
    range is 100, as expected
    InvertedValue is 50
    ProgressBar1.Value = 0

  4. #4
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    372

    Re: progressbar with decreasing values

    Maybe I'm missing something, but it seems like all you would need is the below:

    Code:
        Sub SetInvertedProgress(ByVal value As Integer)
            Dim invertedValue = ProgressBar1.Maximum - value
            ProgressBar1.Value = (ProgressBar1.Minimum + invertedValue)
        End Sub
    If there is ever a chance that the passed progress value is outside the bounds of the ProgressBar's max or min, then you should add some additional logic to cover those cases.

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: progressbar with decreasing values

    I am dumb and didn't account for the fact that we're talking about a percentage in the results and need a value. I think there's enough information in #2 to realize the error but to be clear the correct final snippet would be what OptionBase1 posted.

    See how the "final value" in the original snippet looks more like 'percent'? That was the hint I made a mistake.

    OptionBase1 is correct, we don't need a range here. I'm used to thinking about ranges because, in general, I'm used to progress bars that have a range of 0.0-1.0. In general, you can find the value you shoot for via percentage * range + Minimum.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2015
    Location
    Connecticut, USA
    Posts
    139

    Re: progressbar with decreasing values

    Thank you both very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width