Click here to Skip to main content
15,353,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help making a textbox accept only numbers. I use On second thought, it would be nice if it can accept decimals and negative numbers, too.
Updated 12-Aug-13 18:01pm
Shahan Ayyub 13-Aug-13 2:26am
Try using my approach...

In short you can try something like..
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
           e.Handled = True
        End If

    End Sub

Also see these..
How to create a text box that accepts only numbers and full stop in VB.Net[^]
ridoy 10-Aug-13 1:49am
Thanks SA,:)
Aydin Homay 10-Aug-13 4:23am
My vote of 5 Sergey ;)
Thank you, Aydin.
Use a Regex validator that .Net offers.

If you don't know what that is, use a search engine and get your "learn" on.
In the .net toolbox, is the control, NumericUpDown. It's so very handy.

You can set it to accept decimals of x length or integer.
Access it's value by, X as integer = NumUpDown.Value.
Try using MaskedTextBox[^] instead and specify format for numbers only.
Use if(Decimal.TryPharse(string entedvalue, out resultvalue) == true)
then decimal nuber or number

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900