1

我们如何检查文本框中的负值?我只能 TryParse 文本框,以便在那里验证它是否是数值:

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

是否可以在 TryParse 方法中检查负值?

4

1 回答 1

4

您的If情况基本上是说它是否没有成功将其解析为数字。你想要这样的东西:

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

请注意Else子句、条件的反转以及Decimal.TryParse“非正数”部分中的 return 语句。

于 2013-11-08T20:31:15.293 回答