Hi. I need TextBox in which user can input only numbers. I found some examples about it, but can't do them. For example in the following code

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

If Not (e.KeyChar.IsDigit(e.KeyChar)) And _
e.KeyChar <> ChrW(Keys.Back) Then
e.Handles = True
End If

End Sub

VB underline this
e.Handles = True ' because Handles is not member of "System.Windows.Forms.KeyPressEventArgs"

you could always set up a select case statement for every time the user inputs a value and just set up the case statements to accept only numbers.

You must to specify the character in ascci that contains numbers only
Try This following code :

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

Jery
Best Regards

try this,it will definatly work

Private Function TrapKey(ByVal KCode As String) As Boolean
        If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
            TrapKey = False
        Else
            TrapKey = True
        End If
    End Function


//textbox keypress event
   e.Handled = TrapKey(Asc(e.KeyChar))
Comments
Works fine for me, thank you.

i think you can see ascii table and find the ascii character as you want.
Ok.
all for the best

if u are using vb.net , it should have a default function name IsNumeric

If Not Isnumeric(txtInput.text) then
messagebox.show("Please enter Number")

end if

hi! nice.. i hv tried some codes given here. it's works .

another question,

WHAT IS THE CODE FOR ASSIGN THE USER TO ENTER ONLY THE CHARACTER. NOT A NUMBER???

pLS HELP

THANKS IN ADVANCED

hi! nice.. i hv tried some codes given here. it's works .

another question,

WHAT IS THE CODE FOR ASSIGN THE USER TO ENTER ONLY THE CHARACTER. NOT A NUMBER???

pLS HELP

THANKS IN ADVANCED

Hi,

Try This :

Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
        If e.KeyChar <> ChrW(Keys.Back) Then
            If (e.KeyChar.ToString >= "A" And e.KeyChar.ToString <= "Z") Or (e.KeyChar.ToString >= "a" And e.KeyChar.ToString <= "z") Then
            Else
                e.Handled = True
            End If
        End If
    End Sub

Regards
Veena

Hi,

Try This :

Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
        If e.KeyChar <> ChrW(Keys.Back) Then
            If (e.KeyChar.ToString >= "A" And e.KeyChar.ToString <= "Z") Or (e.KeyChar.ToString >= "a" And e.KeyChar.ToString <= "z") Then
            Else
                e.Handled = True
            End If
        End If
    End Sub

Regards
Veena

Thanks a alot...=)

Also you can do with this code :

Private Sub txtBahasa_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBahasa.KeyPress
        If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
              Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
              And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
              Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
            'space accepted
            If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
                e.Handled = True
            End If
        End If
        If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
            e.Handled = False
        End If

    End Sub

Static Dim flag As Integer
Dim t As New TextBox
t = sender
If InStr(t.Text, ".") = 0 Then
flag = 0
End If
If Not ((Asc(e.KeyChar.ToString) >= 48 And Asc(e.KeyChar.ToString) <= 58) Or (Asc(e.KeyChar.ToString) = 8 Or (Asc(e.KeyChar.ToString) = 46) And flag <> 10)) Then
e.Handled = True
End If
If (Asc(e.KeyChar.ToString) = 46) Then
flag = 10
End If

This article has been dead for over six months. Start a new discussion instead.