Hi
I've got many textfields which are limited to numbers being entered with the following in the KeyPress handler (got this code from internet)
Dim KeyAscii As Short = Asc(e.KeyChar)
Select Case KeyAscii
Case System.Windows.Forms.Keys.
Back '<--- this is for backspace
Case 46 'System.Windows.Forms.Keys
.Decimal '<--- this is for point key
'no decimals already and not first decimal
If sender.Text.IndexOf(".") = -1 And sender.Text.Length > 0 Or sender.selectionstart = 0 Then
Exit Sub
Else
e.Handled = True
TestForm.IsChangesMade = True
Exit Sub
End If
Case 48 To 57 '<--- this is for numbers
Exit Sub
Case 45 '<-- for negatives, allow at first position and only 1 negative
If sender.Text.IndexOf("-") = -1 And sender.Text.Length > 0 Then
Exit Sub
Else
e.Handled = True
Exit Sub
End If
Case Else 'cancel key entry
e.Handled = True
End Select
This works fine. However it means that keyboard shortcuts don't work.
Help please as I'd like the cut, copy and paste still to work.
Thanks in advance.
Start Free Trial