简体   繁体   中英

Masked textbox issue in VB.NET

I have two masked textboxes that have validation if they are valid dates.

Here is the code for the event of the two controls.

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

Here is my code for checking the valid date:

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

When I am testing the valid date and enter value "1212", and I press enter, it prompts the user that the date is not valid, and then when I enter the value "1212" again, the output is not the same. It removes the first character that I entered, and the value now is " 212".

There are no problems when I enter the value "1212" and click the other control. It will validate that the date is not valid, because it leaves the control and do the code behind using the Leave event, but when I always press keydown, it always removes the first character that I entered.

尝试使用jQuery插件掩盖日期。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM