I don't want to TextBox input some text?

isawa

Active member
Joined
Oct 3, 2005
Messages
25
Programming Experience
Beginner
I want to TextBox input only 0-9. How can? show me some code.:eek:
 
In the textbox keypress declaration:

VB.NET:
        If Char.IsControl(e.KeyChar) = False And _
        Char.IsDigit(e.KeyChar) = False Then
            e.Handled = True
        End If

However this wont accept decimal points.

Hope this is of some help
 
You can also find the ascii codes for the values that you want to allow to be entered and make it like in the code below
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TextBox1_KeyPress([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyPressEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TextBox1.KeyPress
[/SIZE][SIZE=2][COLOR=#0000ff]   If[/COLOR][/SIZE][SIZE=2] e.KeyChar <= Chr(47) [/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] e.KeyChar >= Chr(58) [/SIZE][SIZE=2][COLOR=#0000ff]Then [COLOR=darkgreen]'only 0-9[/COLOR]
[/COLOR][/SIZE][SIZE=2]         e.Handled = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

HTH
Regards ;)
 
Thanks, everyone but i used that code just ok. so, i want to know more help about Regex.IsMatch how can find that?

code:
Private Function IsValidNum(ByVal strIn As String) As Boolean
Return Regex.IsMatch(strIn, "^-?\d+(\.\d{2})?$")
End Function
 
Back
Top