Make Textbox Numerical values only?

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
5 posts Page 1 of 1
Contributors
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

How can i make a textbox only accept numerical values( no letters)?
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

Code: Select all
    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

    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = TrapKey(Asc(e.KeyChar))
    End Sub
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

under textbox keypress
Code: Select all
If Char.IsDigit(e.KeyChar) And Char.IsControl(e.KeyChar) Then
e.Handled = True
End if
I got this from a source I found somewhere (I don't know where exactly)
What it does is it checks if the pressed key is a number then the thing that should've happened will actually happen else not :D
Tell me if it doesn't work cooll;
http://vagex.com/?ref=25000
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Axel wrote:
under textbox keypress
Code: Select all
If Char.IsDigit(e.KeyChar) And Char.IsControl(e.KeyChar) Then
e.Handled = True
End if
I got this from a source I found somewhere (I don't know where exactly)
What it does is it checks if the pressed key is a number then the thing that should've happened will actually happen else not :D
Tell me if it doesn't work cooll;
Sure doesn't :(
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Codex's example worked for me.
You could also do it this way:
Code: Select all
e.Handled = Not Char.IsNumber(e.KeyChar)
5 posts Page 1 of 1
Return to “Tutorial Requests”