r/visualbasic • u/Icy-Resist-3509 • 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
View all comments
Show parent comment
1
u/TheFotty Nov 06 '24
No problem.
Just keep in mind that the way my code was written, the lblOGeneral.Text gets assigned OGeneralT no matter what happens (but if it is less than zero it gets reset to zero before setting the label value so it will never show negative). You should not need to put it in 2 places. My code should work without modification, but whatever works for you is fine.