user1699905
user1699905

Reputation: 17

VB.NET: How to validate a textbox to not allow decimal values?

I would like to know how i can validate a textbox to not allow any decimal values?

Upvotes: 0

Views: 2073

Answers (3)

the_lotus
the_lotus

Reputation: 12748

If you can, use a MaskedTextBox

Since handling the KeyPress can cause problem with delete/backspace/copy/paste/...

Upvotes: 1

Mark Hall
Mark Hall

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

Patrick Guimalan
Patrick Guimalan

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

Related Questions