验证数字范围1-100的文本框输入

时间:2017-01-24 20:51:44

标签: arrays vb.net visual-studio validation

我正在尝试验证输入以仅接受1-100的数字范围。我有它接受1-100范围内的数字。我需要的是如何确保不能使用诸如字母或符号(?,/,<,>等等)之类的字符输入。我正在使用循环来查看放入数组的每个输入。当输入被验证时,我返回一个blnOk = True以在我的按钮事件中使用来运行程序。我已经尝试过double.TryParse,但我无法使用它来处理数组。我也有Option Strict ON。 dblStudentTestScores(i)是存储输入的数组.txtTestScores(i)数组用于告诉程序循环所在的文本框,以便它可以通过错误获得焦点。

Private Sub ValidateScores()

    For i = 0 To 8
        If dblStudentTestScores(i) >= 0 And dblStudentTestScores(i) <= 100 Then
            blnOK = True

        Else
            MessageBox.Show("Please Enter Test Score between 0 and 100")
            txtTestScores(i).Clear()
            txtTestScores(i).Focus()
            txtTestScores(i).BackColor = Color.Yellow
            blnOK = False
            Exit Sub
        End If
        txtTestScores(i).BackColor = Color.White
    Next
    blnOK = True
End Sub

这是带有IsNumeric的代码,但它不会进入Else子句。它跳进了try catch。我真的希望它能够专注于具有无效输入的文本框。就像数字不在0到100之间一样。

 Private Sub ValidateScores()

    For i = 0 To 8
        If CInt(IsNumeric(dblStudentTestScores(i))) >= 0 And CInt(IsNumeric(dblStudentTestScores(i))) <= 100 Then
            blnOK = True

        Else
            MessageBox.Show("Please Enter Test Score between 0 and 100")
            txtTestScores(i).Clear()
            txtTestScores(i).Focus()
            txtTestScores(i).BackColor = Color.Yellow
            blnOK = False
            Exit Sub
        End If
        txtTestScores(i).BackColor = Color.White
    Next
    blnOK = True
End Sub

每个输入都会填充数组。

Public Sub PopulateTestScores()
    'Student 1 test scores
    dblStudentTestScores(0) = CDbl(txtStudent1Score1.Text)
    dblStudentTestScores(1) = CDbl(txtStudent1Score2.Text)
    dblStudentTestScores(2) = CDbl(txtStudent1Score3.Text)
    'Student 2 test scores
    dblStudentTestScores(3) = CDbl(txtStudent2Score1.Text)
    dblStudentTestScores(4) = CDbl(txtStudent2Score2.Text)
    dblStudentTestScores(5) = CDbl(txtStudent2Score3.Text)
    'Student 3 test scores
    dblStudentTestScores(6) = CDbl(txtStudent3Score1.Text)
    dblStudentTestScores(7) = CDbl(txtStudent3Score2.Text)
    dblStudentTestScores(8) = CDbl(txtStudent3Score3.Text)
End Sub

2 个答案:

答案 0 :(得分:1)

您可以使用isNumeric功能检查输入。

https://msdn.microsoft.com/en-us/library/6cd3f6w1(v=vs.90).aspx

答案 1 :(得分:1)

您应该使用double.TryParse,因为如果输入不是有效的double,则返回false而不是抛出异常。

Public Function PopulateTestScores() as Boolean
    Dim ok as Boolean
    ok = CheckInput(txtStudent1Score1.Text, 0)
    if not ok Then  
        txtStudent1Score1.Focus()
        return ok
    End if

    ok = CheckInput(txtStudent1Score2.Text, 1)
    if not ok Then  
        txtStudent1Score2.Focus()
        return ok
    End if
    .....
    return ok
End Function

private Function CheckInput(input as String, index as integer) as Boolean
    Dim d as Double        
    Dim ok as Boolean
    ok = double.TryParse(input, d)
    if ok Then dblStudentTestScores(index) = d
    return ok
End Function

具有两个参数的TryParse版本要求字符串与您的语言环境的格式相同(小数点/逗号)。如果你想使用不同的文化,那么只需使用适当的TryParse重载,它也需要CultureInfo参数。

例如,如果使用带有NumberStyles和CultureInfo的重载

ok = Double.TryParse(input, NumberStyles.None, CultureInfo.CurrentCulture, d)

如果输入与您的CultureInfo和数字分组的正确约定不匹配,TryParse将返回false。

en-US文化中的10.1有效双重无效it-IT正确输入为10,1