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.
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
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 

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
under textbox keypress
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
Tell me if it doesn't work cooll;
Code: Select all
I got this from a source I found somewhere (I don't know where exactly)If Char.IsDigit(e.KeyChar) And Char.IsControl(e.KeyChar) Then
e.Handled = True
End if
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

Tell me if it doesn't work cooll;
Axel wrote:under textbox keypressSure doesn't :(Code: Select allI got this from a source I found somewhere (I don't know where exactly)If Char.IsDigit(e.KeyChar) And Char.IsControl(e.KeyChar) Then e.Handled = True End if
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
Tell me if it doesn't work cooll;
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that 

Codex's example worked for me.
You could also do it this way:
You could also do it this way:
Code: Select all
e.Handled = Not Char.IsNumber(e.KeyChar)
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023