VB.NET中的蒙面文本框问题

时间:2014-02-25 03:47:54

标签: vb.net maskedtextbox

我有两个屏蔽文本框,如果它们是有效日期,则会进行验证。

以下是两个控件事件的代码。

Private Sub txtCutOff_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
  Handles txtCutOff.KeyDown

    If e.KeyCode = Keys.Enter Then

        txtPayPeriod.Focus()
        txtPayPeriod.SelectAll()
    End If
End Sub

Private Sub txtCutOff_Leave(sender As Object, e As System.EventArgs) _
  Handles txtCutOff.Leave

    If isClosing = False And isAddEdit And btnCancel.Focused = False Then

        If txtCutOff.Text.Contains(" ") Or txtCutOff.Text.Length <> 10 Then

            MessageBox.Show("Enter Valid Cut Off Date", _
                            "RMI", _
                            MessageBoxButtons.OK, _
                            MessageBoxIcon.Warning)
            txtCutOff.SelectAll()
            txtCutOff.Focus()

            isField_Empty = True
        Else
            ' Get date details
            get_DateDetails(txtCutOff.Text)

            If IsDate("#" & sMonth & "/" & sDay & "/" & sYear & "#") = False Then
                MessageBox.Show("Enter Valid Cut Off Date", _
                               "RMI", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Warning)
                txtCutOff.SelectAll()
                txtCutOff.Focus()

                isField_Empty = True
            Else
                isField_Empty = False
            End If
        End If
    End If
End Sub

Private Sub txtPayPeriod_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
  Handles txtPayPeriod.KeyDown

    If e.KeyCode = Keys.Enter Then

        txtSewers.Focus()
        txtSewers.SelectAll()
    End If
End Sub

Private Sub txtPayPeriod_Leave(sender As Object, e As System.EventArgs) _
  Handles txtPayPeriod.Leave

    If isClosing = False And isAddEdit And btnCancel.Focused = False Then

        If txtPayPeriod.Text.Contains(" ") Or txtPayPeriod.Text.Length <> 10 Then
            MessageBox.Show("Enter Valid Cut Off Date", _
                            "RMI", _
                            MessageBoxButtons.OK, _
                            MessageBoxIcon.Warning)
            txtPayPeriod.SelectAll()
            txtPayPeriod.Focus()

            isField_Empty = True
        Else
            ' Get date details
            get_DateDetails(txtPayPeriod.Text)

            If IsDate("#" & sMonth & "/" & sDay & "/" & sYear & "#") = False Then
                MessageBox.Show("Enter Valid Cut Off Date", _
                                "RMI", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Warning)
                txtPayPeriod.SelectAll()
                txtPayPeriod.Focus()

                isField_Empty = True
            Else
                isField_Empty = False
            End If
        End If
    End If
End Sub

以下是我检查有效日期的代码:

Sub get_DateDetails(strDate)

    ' Get month
    sMonth = strDate.Remove(0, 5)
    sMonth = sMonth.Remove(2, 3)

    ' Get day
    sDay = strDate.Remove(0, 8)

    ' Get year
    sYear = strDate.Remove(4, 6)
End Sub

当我测试有效日期并输入值“1212”时,按Enter键会提示用户日期无效,然后当我再次输入值“1212”时,输出不是相同。它删除了我输入的第一个字符,现在值为“212”。

输入值“1212”并单击另一个控件时没有问题。它将验证日期无效,因为它离开控件并使用Leave事件执行代码,但是当我总是按下keydown时,它总是删除我输入的第一个字符。

1 个答案:

答案 0 :(得分:0)

尝试使用jQuery插件来屏蔽日期。