954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

numeric TextBox

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"

Annex
Newbie Poster
16 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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.

drew.haley
Newbie Poster
17 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

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))
poonams
Newbie Poster
23 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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

Sawamura
Junior Poster in Training
69 posts since Nov 2007
Reputation Points: 55
Solved Threads: 6
 

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

chewmp
Newbie Poster
4 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

You just need to handle the KeyChar

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

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

nasren7585
Newbie Poster
3 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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

nasren7585
Newbie Poster
3 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

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

nasren7585
Newbie Poster
3 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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
Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

Or just do this:

if Isnumeric(String) then
do_stuff_here
end if

drew.haley
Newbie Poster
17 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

if you are using VB2005 OR VB2008 simply use this valitation code into your textbox change event. original source ( http://developerskb.blogspot.com/2008/09/how-to-create-numeric-textbox-in-vbnet.html )

If Not IsNumeric(Me.TxtBox1.Text) Then
     MsgBox("Please enter numeric value only", MsgBoxStyle.Information)
     Me.TxtBox1.Text = 0
End If
arbalu
Newbie Poster
4 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

HI,
HOPE THE BELOW LINK WILL HELP READERS A LOT FOR CROSS-BROWSER NUMERIC VALIDATION:
http://shawpnendu.blogspot.com/2009/03/cross-browser-javascript-isnumeric.html

mail2saion
Posting Whiz in Training
247 posts since Apr 2009
Reputation Points: 26
Solved Threads: 44
 

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

risingsunankur
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You