-1

I created a simple program called poodle doodle where you just click buttons to add to a progress bar but they could easily crash the program by just going 1 over the max amount but i cant figure out how to stop it from doing that. How can I?

Here's my current code

Public Class Form1

Private Sub poodle_Click(sender As Object, e As EventArgs) Handles poodle.Click
    doodle.Visible = True
    poodle.Visible = False
    pdb1.Value = pdb1.Value + 1
End Sub

Private Sub doodle_Click(sender As Object, e As EventArgs) Handles doodle.Click
    poodle.Visible = True
    doodle.Visible = False
End Sub

Private Sub pdb1_Click(sender As Object, e As EventArgs) Handles pdb1.Click
    If pdb1.Value = 100 Then
        MessageBox.Show("You Poddled And Doodled To Victory!", "Poodle Doodle Champion")
        pdb1.Value = pdb1.Value = 0
    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    pdb1.Maximum = TextBox1.Text
End Sub
Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68
Leon
  • 11
  • 2
  • check the value before you use it – Ňɏssa Pøngjǣrdenlarp Jun 21 '15 at 18:04
  • You can also call the `PerformStep` method instead of setting the `Value` property explicitly. When you call `PerformStep`, the `Value` will never go beyond the bounds of the `Minimum` and `Maximum`. Of course, if you're actually letting the user try to go beyond those bounds then you probably haven't written very good code. The obvious option seems to be to disable the `Button` that increments the bar if the bar can't actually be incremented. – jmcilhinney Jun 22 '15 at 00:48

1 Answers1

0

Change the poodle_Click method to check that the value is less than 100 like this:

Private Sub poodle_Click(sender As Object, e As EventArgs) Handles poodle.Click
    doodle.Visible = True
    poodle.Visible = False

    ' This bit
    If pdb1.Value < 100 Then
        pdb1.Value += 1
    End If

End Sub

You'll probably want to add other logic to disable the poodle/doodle buttons when the player wins and a way to restart the game.

I'm not sure why you put pdb1.Value = pdb1.Value = 0 in the pdb1_Click method, it should just be pdb1.Value = 0.

Equalsk
  • 7,954
  • 2
  • 41
  • 67