-3

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!

2 Answers2

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
-1

Take a look to the following links:

http://www.vbforums.com/showthread.php?570438-Restrict-TextBox-to-only-certain-characters-numeric-or-symbolic

http://www.codigofacilito.com/videos/visual_basic_net_parte_funcion_para_recibir_solo_caracteres_curso_vbnet

If you still have any doubt, let me know.