Help, Only numbers, no letters

Posts 1–13 of 13 · Page 1 of 1
Help, Only numbers, no letters
Hi!

I need to make a textbox only accept numbers! No letters or special symbols!

//H
masked textbox?
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim NumbersAccepted As String = "0123456789" 'or the numbers you want to use

    If NumbersAccepted.IndexOf(e.KeyChar) = -1 Then
        e.Handled = True
    End If
Quote Originally Posted by Lolland View Post
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim NumbersAccepted As String = "0123456789" 'or the numbers you want to use

    If NumbersAccepted.IndexOf(e.KeyChar) = -1 Then
        e.Handled = True
    End If
DAM for a 12 year old your beast at VB

PS you should make a multitool.
Quote Originally Posted by Lolland View Post
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim NumbersAccepted As String = "0123456789" 'or the numbers you want to use

    If NumbersAccepted.IndexOf(e.KeyChar) = -1 Then
        e.Handled = True
    End If
Awsome thanks! It works only numbers and no letters! Thanks!

But i can´t use backspace now :/ u know how to fix?
I would, but I prefer smaller projects.
Yeah, that's the only thing that sucks about that method. There's other better ways of doing it, I'll check my project folder (about 100 projects )
Quote Originally Posted by Lolland View Post
Yeah, that's the only thing that sucks about that method. There's other better ways of doing it, I'll check my project folder (about 100 projects )
I think this may be why you can't backspace:
Code:
If NumbersAccepted.IndexOf(e.KeyChar) = -1 Then
        e.Handled = True
I'm not sure, but you should probably look at it...
I have another possible solution, but I don't feel like posting it right now
Just PM me and I will answer it later (I'm too busy unpacking stuff from vacation)
Problem solved //Close

Found a other way that worked much better Thans btw
What was it? I know I have it somewhere, but I've gone through 30 projects in like 2 hours and I don't wanna check the rest -.-
Post the code, I know I have it but I cant find it :@
VBVBVBVBVBVBVBVBVBVBVBVBVBVBVBVBVBVBVBVBVB
Quote Originally Posted by Lolland View Post
Post the code, I know I have it but I cant find it :@
Sure,

Code:
 Private Function ValidateNumeric(ByVal strText As String) _
        As Boolean
        ValidateNumeric = CBool(strText = "" _
            Or strText = "-" _
            Or strText = "-." _
            Or strText = "." _
            Or IsNumeric(strText))
    End Function
Code:
  If Not ValidateNumeric(TextBox1.Text) Then

code here

end if
cool code.
Posts 1–13 of 13 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us