0

我在 VB.NET 中有 4 个textboxes,我必须在其中验证用户输入。根据输入到文本框中的数据,我必须显示以下消息之一:

分数 1 为空白

分数 1 不是数字

分数 1 不在范围内:0-10

分数 1 有效

我必须对 、 和 重复Score2Score3操作Score4

这是我的代码:

Public Class Form1
    Dim vintScore As Integer = 1

    Private Sub btn_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Clear.Click
        Call Clear()
    End Sub

    Private Sub btn_DefaultValues_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_DefaultValues.Click
        Call DefaultValues()
    End Sub

    Private Sub btn_Validate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Validate.Click
        Call ValidateData(txt_score1.Text)
        Call ValidateData(txt_score2.Text)
        Call ValidateData(txt_score3.Text)
        Call ValidateData(txt_score4.Text)
    End Sub

    Private Sub Clear()
        txt_score1.Text = ""
        txt_score2.Text = ""
        txt_score3.Text = ""
        txt_score4.Text = ""
        txt_category.Text = ""
        lbl_StatusContent.Text = "All Data Cleared!"
    End Sub

    Private Sub DefaultValues()
        Call Clear()
        txt_score1.Text = "4"
        txt_score2.Text = "1"
        txt_score3.Text = "2"
        txt_score4.Text = "4"
        txt_category.Text = "A"
        lbl_StatusContent.Text = vbCrLf & "TextBox values set to default values"
    End Sub

    Private Sub ValidateData(ByVal vScore As String)
        If (vScore = "") Then
            lbl_StatusContent.Text = vbCrLf & "Score " & vintScore & " is blank"
        ElseIf Not (IsNumeric(vScore)) Then
            lbl_StatusContent.Text = vbCrLf & "Score " & vintScore & " is not numeric"
        ElseIf (vScore < 0 And vScore > 10) Then
            lbl_StatusContent.Text = vbCrLf & "Score " & vintScore & " is not in range: 0-10"
        Else
            lbl_StatusContent.Text = vbCrLf & "Score " & vintScore & " is Valid"

        End If
        vintScore = vintScore + 1
    End Sub
End Class

我目前得到的输出是:

4分有效

8分有效

分数 12 不是数字

等等...

而我所追求的是:

分数 1 有效

分数 2 不是数字

分数 3 不在范围内:0-10

4分有效

像这样...

4

2 回答 2

0

将数字作为参数传递:

Private Sub ValidateData(ByVal vScore As String, ByVal vintScore as String)
    If (vScore = "") Then
        lbl_StatusContent.Text = vbCrLf & "Score " & vintScore & " is blank"
    ElseIf Not (IsNumeric(vScore)) Then
        lbl_StatusContent.Text = vbCrLf & "Score " & vintScore & " is not numeric"
    ElseIf (vScore < 0 And vScore > 10) Then
        lbl_StatusContent.Text = vbCrLf & "Score " & vintScore & " is not in range: 0-10"
    Else
        lbl_StatusContent.Text = vbCrLf & "Score " & vintScore & " is Valid"
    End If
End Sub

来电者:

Private Sub btn_Validate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Validate.Click
    Call ValidateData(txt_score1.Text, "1")
    Call ValidateData(txt_score2.Text, "2")
    Call ValidateData(txt_score3.Text, "3")
    Call ValidateData(txt_score4.Text, "4")
End Sub
于 2013-09-07T11:55:40.437 回答
0

正如我在评论中建议的那样,使用StringBuilder

Private Sub btn_Validate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Validate.Click
    Dim sb As New StringBuilder()
    Call ValidateData(txt_score1.Text, 1, sb)
    Call ValidateData(txt_score2.Text, 2, sb)
    Call ValidateData(txt_score3.Text, 3, sb)
    Call ValidateData(txt_score4.Text, 4, sb)
    lbl_StatusContent.Text = sb.ToString()
End Sub

Private Sub ValidateData(ByVal vScore As String, ByVal vintScore As Integer, sb As StringBuilder)
    If vScore = "" Then
        sb.AppendLine("Score " & vintScore & " is blank")
    ElseIf Not (IsNumeric(vScore)) Then
        sb.AppendLine("Score " & vintScore & " is not numeric")
    '...
End Sub
于 2013-09-07T14:56:16.767 回答