r/visualbasic • u/rodiqio • Dec 11 '20
How to keep an nput box within a range
So I'm making a random guessing game, the thing is that instead of putting my answer in a textbox it I have to put in a inputbox. I got most of the code to work how I want it, but I still need to make it so that the guess is invalid if 0 or lower or 101 and higher. Also if it was a regular textbox I could use tryparse to validate responses as an integer, but I have no idea how to go about it in an Inputbox. Any and all help appreciated. Btw the random number is generated in another sub that calld the one featured in the pic it's intGeneratedNum = randomNum.Next(1,100)

6
Upvotes
2
u/Hounish Dec 12 '20
Here's a quick and dirty snippet i came up with.. Please note that i haven't used an inputbox in ages!
Private Sub EasyMode()Dim intUser_Guess As String = InputBox("Guess a number between 1 and 100")If Not IsNumeric(intUser_Guess) ThenMsgBox("Please enter a numeric value")EasyMode()End IfIf intUser_Guess < 0 OrElse intUser_Guess > 100 ThenMsgBox("Please enter a value between 1 and 100!")EasyMode()End If' Message box the value...If Not String.IsNullOrEmpty(intUser_Guess) Then MsgBox(intUser_Guess)End SubWhat this does, is first check if the value entered was numeric and re-calls the sub if not, then checks if the value is within the range and again, re-calls the Sub if not.
Maybe not the best solution, but it should give you an idea on how to proceed from there..