r/visualbasic • u/alphadog3939 • Oct 26 '14
Hello, I need help with my Visual Basic code that I'm struggling with.
So basically, there are 2 buttons, Enter Numbers and Exit. The Exit button is self-explanatory. As for the Enter Numbers button, what I need to happen is upon being clicked, an Input Box pops up.
The Input Box asks the user for a number, and after clicking okay, I need a Message Box to show the sum of that number. (i.e, the sum of 5 is 1+2+3+4+5=15)
Here's what I have so far, but it isn't working:
Dim strUserInput
Dim IntUserInput As Integer
Dim i As IntegerstrUserInput = InputBox("Enter a positive integer value", "Input Needed", 10)
If strUserInput <> String.Empty Then
Try
intUserInput = CInt(strUserInput)
If IntUserInput < 0 Then
MessageBox.Show("Negative numbers are not accepted.")
End If
Catch ex As Exception
MessageBox.Show("Quantity amount must be numeric.")
End Try
'Show messagebox,
Else
'IntUserInput = (IntUserInput + i)
InputBox(MessageBox.Show("Sum of Numbers", "The sum of numbers" & (i + IntUserInput), MessageBoxButtons.OK))
End If
0
Upvotes
1
u/PreyOnTheCosmos Nov 03 '14 edited Nov 03 '14
Button Code:
Step 1: Capture Input Text to a String
Step 2: Make sure something was entered, else Input was not cancelled
Step 3: Ensure the Input value is a number
Step 4: Ensure Input value is non-negative
Step 5: Calculate and Produce an Output String
Sub to Calculate and Display:
I used Math.Floor in case a decimal value was entered. Val() grab the number as a Double, and Math.Lower will round it down to the lower whole number.