Click here to Skip to main content
15,886,873 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 vb.net. On second thought, it would be nice if it can accept decimals and negative numbers, too.
Posted
Updated 12-Aug-13 18:01pm
v2
Comments
Shahan Ayyub 13-Aug-13 2:26am    
Try using my approach...

In short you can try something like..
VB
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[^]
Vb-net-need-text-box-to-only-accept-numbers[^]
How-to-allow-user-to-enter-only-numbers-in-a-textbox-in-vb-net[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 9-Aug-13 17:04pm    
5ed.
—SA
ridoy 10-Aug-13 1:49am    
Thanks SA,:)
Aydin Homay 10-Aug-13 4:23am    
My vote of 5 Sergey ;)
Sergey Alexandrovich Kryukov 10-Aug-13 6:37am    
Thank you, Aydin.
—SA
Use a Regex validator that .Net offers.

If you don't know what that is, use a search engine and get your "learn" on.
 
Share this answer
 
v5
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.
 
Share this answer
 
Try using MaskedTextBox[^] instead and specify format for numbers only.
 
Share this answer
 
Use if(Decimal.TryPharse(string entedvalue, out resultvalue) == true)
then decimal nuber or number
 
Share this answer
 

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