我在 VB.NET 中有 4 个textboxes
,我必须在其中验证用户输入。根据输入到文本框中的数据,我必须显示以下消息之一:
分数 1 为空白
分数 1 不是数字
分数 1 不在范围内:0-10
分数 1 有效
我必须对 、 和 重复Score2
此Score3
操作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分有效
像这样...