我正在开发 Windows 应用程序。我有一个表单,我正在尝试验证该表单上的文本框。
我想对文本框进行一些验证,例如文本框应该只接受 Alphabates、Didgits 和逗号。(没有其他字符,如特殊符号。)同样,当光标在该文本框中时,它应该接受 Enter 键。
我正在尝试编写代码,但有些代码无法正常工作。但它仍然采用特殊字符,如 <>/;' 我必须做出哪些改变?
这是代码...
按键按下事件
Private Sub txtOLDBuildingName_KeyDown(sender As Object, e As KeyEventArgs) Handles txtOLDBuildingName.KeyDown
' Initialize the flag to false.
nonNumberEntered = False
' Determine whether the keystroke is a number from the top of the keyboard.
If (e.KeyCode < Keys.D0 And e.KeyCode > Keys.D9) And (e.KeyCode > Keys.A And e.KeyCode < Keys.Z) Then
nonNumberEntered = True
End If
'If shift key was pressed, it's not a number.
If Control.ModifierKeys = Keys.Shift Then
nonNumberEntered = True
End If
End Sub
按键事件
Private Sub txtOLDBuildingName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtOLDBuildingName.KeyPress
If nonNumberEntered = True Then
e.Handled = True
End If
End Sub