Why are you using a Textbox? Just use a NumericUpDown control with the default set to 0. Not only will this correct your immediate problem, it also saves you all kinds of trouble in verifying that the "values" are actually numeric.
deceptikon
Challenge Accepted
3,452 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
An simpler way, Dim num1 as Double = Val(TextBox1.Text) the Val function returns a double, but if you need it to be an Integer, you can convert it to Integer, Dim num1 as Integer = Convert.ToInt16(Val(TextBox1.Text)) It will convert any string representation of a number to a number, and if the string starts with any non digit character, or is null it will return 0
Or you could use a MaskedTextBox. It allows you to use a format string to restrict what is typed in. Something like "####0.00", will allow any digits to be entered, use the 0 to hard code the format say you want a minimum number of digits, and or decimal places. Use the # to soft code it, to restrict the mamximum number of digits, but not require a minimum. As per my example the user will have to submit at least 3 digits, 2 of them decimnal, but will allow up to 7 digits.
tinstaafl
Nearly a Posting Virtuoso
1,329 posts since Jun 2010
Reputation Points: 355
Solved Threads: 233
Skill Endorsements: 14
Dim num1 As Integer
Dim num2 As Integer
If IsNumeric(txtNum1.Text) Then
num1 = CInt(txtNum1.Text)
Else
num1 = 0
End If
If IsNumeric(txtNum2.Text) Then
num2 = CInt(txtNum2.Text)
Else
num2 = 0
End If
Or more concisely
Dim num1 As Integer = 0
If IsNumeric(txtNum1.Text) Then num1 = CInt(txtNum1.Text)
Dim num2 As Integer = 0
If IsNumeric(txtNum2.Text) Then num2 = CInt(txtNum2.Text)
You'll have to modify if the types are different, and you should limit the number of digits that can be entered to prevent overflow.
Reverend Jim
Carpe per diem
3,604 posts since Aug 2010
Reputation Points: 561
Solved Threads: 448
Skill Endorsements: 32