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事件執行代碼,但是當我總是按下按鍵時,它總是刪除我輸入的第一個字符。