I really need to prevent any non-numeric entry (Including paste) to my TextBox and also I want to prevent it from appearing on the TextBox before being erased, It would be wonderful if you put some hints regarding your code suggestion, because I'm just a beginner!
Asked
Active
Viewed 1,748 times
-3
-
1http://stackoverflow.com/questions/938329/how-to-filter-textbox-input-to-numeric-only duplicate – Saechel Aug 28 '14 at 23:31
-
2what disaster lies in store if they do enter a numeral? – Ňɏssa Pøngjǣrdenlarp Aug 28 '14 at 23:37
-
I have viewed those threads before asking my questions, and I'm pretty sure that they are missing one of my points! (Preventing paste in the field) @Saechel – Abdelrahman Bannora Aug 29 '14 at 00:05
-
I can't understand you @Plutonix – Abdelrahman Bannora Aug 29 '14 at 00:07
2 Answers
0
Edited : added CtrlV handling and opimized Keypress event.
You could use this code, wich prevent numeric values from being handled :
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsDigit(e.KeyChar) Then ' Check if the value is a number
e.KeyChar = ChrW(0)
e.Handled = True
End If
End Sub
and for the CtrlV handling you can use this code :
Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
' Checking if control + v is clicked or Shift + Insert is clicked
If (e.Control AndAlso Char.ToLower(ChrW(e.KeyValue)) = "v"c) Or (e.Shift AndAlso e.KeyValue = Keys.Insert) Then
e.Handled = True
e.SuppressKeyPress = True
End If
End Sub

Abdessabour Mtk
- 3,895
- 2
- 14
- 21
-
-
Change `Handles textbox1.Keypress` to `handles yourtextbox.Keypress` – Abdessabour Mtk Aug 29 '14 at 10:14