I'm trying to simulate a game whereby a player can damage an enemy or heal themselves via clicking buttons "Attack" and "Heal". I've tried to do a test-run on one Progress Bar to just make sure everything works before modifying it to fit my game, and made a new WindowsApplication.
I have a Progress Bar on the form along with one Attack button and a Heal button and if I exceed the maximum value of the progress bar by healing or minimum by damaging, it spews out errors. I can't figure out the line of code in which to fix it for both cases and I've been looking everywhere for answers.
Here's my Heal Button Code;
Private Sub btnHeal_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnHeal.Click
prgHealth.Value = prgHealth.Value + 10
If prgHealth.Value >= prgHealth.Maximum Then
prgHealth.Value = prgHealth.Maximum
End If
End Sub
And here's my Damage Button Code;
Private Sub btnDamage_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDamage.Click
prgHealth.Value = prgHealth.Value - 10
If prgHealth.Value < prgHealth.Minimum Then
prgHealth.Value = prgHealth.Minimum
End If
End Sub
Any help would be greatly appreciated. Please also note that I'm a beginner at coding and will definitely get confused with large walls of code.
I'm sure many people who have messed with Progress Bars like this will have had the same problem, and I know it's probably something silly!
Thank ye for helping!
You are doing things in the wrong order. Your first apply the change to the progressbar, then you check if it more than maximum or less then minimum. Think of the progressbar as a glass of water. You cant fill it up over the edge (No smartass comments about Ice now people) because then you'll have water everywhere. You also cannot withdraw more water than what is in the glass (No smartass comments about dehumidifying now please). What you do now is to first fill the progressbar with too much water so it goes over the edge. Then check if you poured to much water into the glass. Rather than meassuring the amount of water before pouring it into the glass. :D
You need to do the check first. Like this:
if prgHealth.Value + 10 > prgHealth.Maximum Then
prgHealth.Value = prgHealth.Maximum
else
prghealth.Value = prgHealth.Value + 10
end if
And then the same for minimum.