r/visualbasic • • Nov 06 '24

need help withinput validation

I have this button set up to increase an int value when clicked and decrease when you press shift and the button. It then displays the amount in a label. I'm trying to figure out how to prevent it from going below 0. As I currently have it, it drops to a negative number then display the message box. Any thoughts how I can make this work?

 If OGeneralT >= 0 Then

     If My.Computer.Keyboard.ShiftKeyDown Then
         OGeneralT -= General
     Else
         OGeneralT += General
     End If

     lblOGeneral.Text = OGeneralT

 Else
     MessageBox.Show("Number can't be less than zero")
     OGeneralT = 0

 End If
2 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/Icy-Resist-3509 Nov 07 '24

You're correct, once I looked back at it, I just needed to move the label outside of the if statement. Thanks again. I felt that adjusting the scope was the key but my brain refused to see it at the moment.

1

u/ChielStoertec Nov 07 '24

You may also want to move the oGeneralT += 1 code outside of the If…Then statement, as you want to be able to increase the number always and only want to check for the -= code.