使用数组验证数字输入

时间:2016-11-26 14:13:06

标签: vb.net

我是一名学习VB.NET的在线学生。我有一个正在进行的项目,需要一些帮助才能开始。

我们正在学习阵列,而且我正在努力学习。我需要使用一个数组创建一个pin验证器,该数组将检查每个输入的数字范围。

该应用程序将有7个文本框。 txtBox1必须在7-9范围内,txtBox2必须在5-7范围内,txtBox3在0-4范围内,txtBox4在0-9范围内,txtBox5在范围6-9中,txtBox 6在3-6范围内,txtBox7 in范围4-8。

单击“验证”按钮时,阵列需要验证每个输入是否在指定范围内,然后在引脚良好时显示消息。

我有代码在运行,但我无法验证错误的输入。我需要验证输入是否为数字,并且它在设置的范围内。如果输入不好,那么我需要文本框突出显示黄色并且该框获得焦点。我尝试了很多IsNumeric的变体,但我无法获得文本框以获得焦点或突出显示。

我希望有人能够清楚地解释代码是如何工作的,这样我就可以继续自己做,并完全理解我在做什么。对此未来程序员的任何帮助将不胜感激。

Option Strict On

Public Class frmPinVerifier
Dim blnOk As Boolean = False

Private Sub btnVerify_Click(sender As Object, e As EventArgs) Handles btnVerify.Click
    'Arrays
    Dim intMinimum() As Integer = {7, 5, 0, 0, 6, 3, 4}
    Dim intMaximum() As Integer = {9, 7, 4, 9, 9, 6, 8}
    Dim strArrayMessage() As String = {"Must be in range of 7 through 9", "Must be in range of 5 through 7", "Must be in range of 0 through 4", "Must be in range of 0 through 9", "Must be in range of 6 through 9", "Must be in range of 3 through 6", "Must be in range of 4 through 8"}
    Dim intTextBox() As Integer = {CInt(txtBox1.Text), CInt(txtBox2.Text), CInt(txtBox3.Text), CInt(txtBox4.Text), CInt(txtBox5.Text), CInt(txtBox6.Text), CInt(txtBox7.Text)}


    'Start Count
    Dim intCount As Integer = 0


    For intCount = 0 To intMinimum.Length - 1 And intMaximum.Length - 1
        If intTextBox(intCount) >= intMinimum(intCount) And intTextBox(intCount) <= intMaximum(intCount) Then
            blnOk = True
        Else

            MessageBox.Show(strArrayMessage(intCount))

        End If

    Next

    If blnOk = True Then
        MessageBox.Show("PIN Verified")
        txtBox1.Clear()
        txtBox2.Clear()
        txtBox3.Clear()
        txtBox4.Clear()
        txtBox5.Clear()
        txtBox6.Clear()
        txtBox7.Clear()


    End If

1 个答案:

答案 0 :(得分:1)

您似乎没有任何代码可以设置焦点或更改相关文本框的背景颜色。

您可以将数组设置为对文本框的引用。这样可以轻松访问这些文本框的属性。您与Dim intTextBox() As Integer = {CInt(txtBox1.Text),...关系密切,但在数组中使用.Text属性有点过于具体。

Private Sub btnVerify_Click(sender As Object, e As EventArgs) Handles btnVerify.Click
    Dim nums = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7}
    Dim intMinimum() As Integer = {7, 5, 0, 0, 6, 3, 4}
    Dim intMaximum() As Integer = {9, 7, 4, 9, 9, 6, 8}
    Dim pinIsValid As Boolean = True

    For Each tb In nums
        tb.BackColor = Color.White
    Next

    For i = 0 To nums.Length - 1
        Dim val = Integer.Parse(nums(i).Text)
        Dim minVal = intMinimum(i)
        Dim maxVal = intMaximum(i)
        If val < minVal OrElse val > maxVal Then
            pinIsValid = False
            nums(i).BackColor = Color.Yellow
            nums(i).Focus()
            'TODO: Adjust "Entry {i + 1}" so it makes sense to the user.
            MessageBox.Show($"Entry {i + 1} must be in the range {minVal} to {maxVal} (inclusive)")
            Exit Sub
        End If
    Next

    If pinIsValid Then
        For i = 0 To nums.Length - 1
            nums(i).Text = ""
        Next
        MessageBox.Show("PIN Verified")
    End If

End Sub