Answered Progress bars...

PRo-Beaniie

Well-known member
Joined
Mar 17, 2011
Messages
55
Location
Hertford, Hertfordshire, United Kingdom
Programming Experience
3-5
Hey Guys,

I had some trouble a while back with my progress bars exeeding their maximum sizes. I though I had fixed the problem but obviously I was wrong every time the maximum size is exeeded the application enteres debugging mode .... this is really frustrating as the progress bars are a major factor in the game...

What I have is one progress bar which is linked to a clock the clock which ticks up to 1:30 and the game then ends. I also have another progress bar showing the health of the shooter.

I have a series of collision codes set up which are linked to these progress bars, that add to them and take from them.
VB.NET:
 'Heath pick up collision code...
        If (HPU.Top + HPU.Height >= Shooter.Top) And (HPU.Top <= Shooter.Top + Shooter.Height) And (HPU.Left + HPU.Width >= Shooter.Left) And (HPU.Left <= Shooter.Left + Shooter.Width) And HPU.Visible = True Then
            HPU.Visible = False
            HPU.Top = -100
            status.Text = status.Text + 25
 
'This is the code in question...
            [COLOR=red]HEALTHBAR.Value = HEALTHBAR.Value + 25[/COLOR]
        Else
            HEALTHBAR.Value = HEALTHBAR.Value + 0
        End If

I have tried re-routing the overload so that if it cannot add no more to the progress bars it adds nothing to them. i could really use some help with this....

Any Ideas ....:confused:
 
Last edited:
Either you have omitted the critical part of the code, i.e. where you check the Maximum, or you have no such check. My guess would be that you are always incrementing by 25, even if the Value is less than 25 below the Maximum. In that case, what you need to do is increment by a value that is either 25 or the difference between the Value and the Maximum, whichever is less.
VB.NET:
myProgressBar.Increment(Math.Min(25, myProgressBar.Maximum - myProgressBar.Value))
That will never allow the Value to exceed the Maximum under any circumstances.
 
Increment method is safe to use for any value and will not exceed the max limit.
Another alternative is to set Step property and use PerformStep method to auto-increment. PerformStep is also safe to use in regard to the max limit.
ProgressBar Class (System.Windows.Forms)
 
Back
Top