案例声明验证

时间:2015-03-22 04:12:24

标签: vb.net validation case case-statement

我需要验证某个文本框输入只接受0-10范围之间的数字数据,这样当按下按钮并且数据不在这些规范范围内时,它会在标签中显示错误消息。 然后,如果数据无效,则清除文本框,聚焦文本框并退出子例程。

目前的代码是:

Select Case decScore1
        Case txtScore1.Text = ""
            strScore1Error = "Score 1 is blank"
            lblStatusShow.Text = strScore1Error
            txtScore1.Clear()
            txtScore1.Focus()
            Exit Sub

        Case Not IsNumeric(txtScore1.Text)
            strScore1Error = "Score 1 is not a numeric value: 0-10"
            lblStatusShow.Text = strScore1Error
            txtScore1.Clear()
            txtScore1.Focus()
            Exit Sub

        Case decScore1 > 10 Or decScore1 < 0
            strScore1Error = "Score 1 is not in the range: 0-10"
            lblStatusShow.Text = strScore1Error
            txtScore1.Clear()
            txtScore1.Focus()
            Exit Sub

        Case Else
            strScore1Error = "Score 1 input is valid"
    End Select`

问题是:

如果txtScore1留空,lblStatusShow =“得分1不在范围内:0-10”当我需要它时,“得分1为空白”

如果txtScore1不是数值,则lblStatusShow =“得分1为空”

如果txtScore1超出0-10范围,则lblStatusShow =“得分1有效”

所以基本上它没有以正确的方式验证,我不确定为什么或如何解决它。

2 个答案:

答案 0 :(得分:0)

您告诉编译器在deScore1中取Select Case的值,然后检查txtScore11.Text的值。

我会建议这个

Dim score As Integer    
With txtScore1
    If IsNumeric(.Text) Then
        strScore1Error = "Score 1 is not a numeric value: 0-10"
        lblStatusShow.Text = strScore1Error
        txtScore1.Clear()
        txtScore1.Focus()
        Exit Sub
    ElseIf .Text.Trim.Length = 0 Then
        strScore1Error = "Score 1 is blank"
        lblStatusShow.Text = strScore1Error
        txtScore1.Clear()
        txtScore1.Focus()
        Exit Sub
    ElseIf Integer.TryParse(.Text, score) Then 
        If (score > 10 or score < 0 Then
            strScore1Error = "Score 1 is not in the range: 0-10"
            lblStatusShow.Text = strScore1Error
            txtScore1.Clear()
            txtScore1.Focus()
            Exit Sub
        End If
    Else
        strScore1Error = "Score 1 input is valid"
    End If
End With

答案 1 :(得分:0)

  Select Case decScore1

            Case 1 To 10
                strScore1Error = "Score "
                lblStatusShow.Text = strScore1Error

            Case Nothing
                strScore1Error = "Blank "
                lblStatusShow.Text = strScore1Error

            Case Is > 10
                strScore1Error = "Score is more "
                lblStatusShow.Text = strScore1Error


            Case Else
                strScore1Error = "Score 1 is not a numeric value: 0-10"
                lblStatusShow.Text = strScore1Error
        End Select