How to check for negative values in text box in VB?

How to we check for negative values in a text box? I was only able to TryParse the text box so that there it will validate it if it's a numeric value:

If Not Decimal.TryParse(txtParts.Text, decParts) Then
    If decParts <= 0 Then
        MessageBox.Show("ERROR: Value must be a positive number!")
    End If
    MessageBox.Show("ERROR: Value must be numeric!")
    Return False
End If

Is it possible to check for negative values inside a TryParse method?

Jon Skeet
people
quotationmark

Your If condition is basically saying if it hasn't successfully parsed it as a number. You want something like:

If Decimal.TryParse(txtParts.Text, decParts) Then
    If decParts <= 0 Then
        MessageBox.Show("ERROR: Value must be a positive number!")
        Return False
    End If
Else
    MessageBox.Show("ERROR: Value must be numeric!")
    Return False
End If

Note the Else clause, and the inversion of the condition for Decimal.TryParse, and the return statement in the "not positive" part.

people

See more on this question at Stackoverflow