Reputation: 17
I would like to know how i can validate a textbox to not allow any decimal values?
Upvotes: 0
Views: 2073
Reputation: 12748
If you can, use a MaskedTextBox
Since handling the KeyPress can cause problem with delete/backspace/copy/paste/...
Upvotes: 1
Reputation: 54562
You can use the KeyPress
event and use the IsNumeric
Function to trap the numeric keys.
Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If IsNumeric(e.KeyChar) Then
e.Handled = True
End If
End Sub
Upvotes: 1
Reputation: 1010
this solution i got from this link ( How to allow user to enter only numbers in a textbox in vb.net? )
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
End Sub
Upvotes: 1