簡體   English   中英

VB.NET中的隱藏的文本框問題

[英]Masked textbox issue in VB.NET

我有兩個帶掩碼的文本框,如果它們是有效日期,則它們具有驗證。

這是兩個控件的事件代碼。

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時,它將始終刪除我輸入的第一個字符。

嘗試使用jQuery插件掩蓋日期。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM