2014-02-25 43 views
1

我有两个屏蔽的文本框有验证,如果他们是有效的日期。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”,我按下回车键,提示用户的日期是无效的,然后当我再次输入值“1212”时,输出不一样。它删除我输入的第一个字符,现在的值是“212”。

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

回答