User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 455,988 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,790 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 5663 | Replies: 13
Reply
Join Date: Oct 2007
Posts: 16
Reputation: Annex is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Annex Annex is offline Offline
Newbie Poster

numeric TextBox

  #1  
Dec 5th, 2007
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"
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Posts: 17
Reputation: drew.haley is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
drew.haley drew.haley is offline Offline
Newbie Poster

Re: numeric TextBox

  #2  
Dec 5th, 2007
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.
Reply With Quote  
Join Date: Nov 2007
Location: � Jogja �
Posts: 2,590
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Rep Power: 11
Solved Threads: 236
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: numeric TextBox

  #3  
Dec 6th, 2007
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
Last edited by Jx_Man : Dec 6th, 2007 at 1:05 am. Reason: explaination
Reply With Quote  
Join Date: Oct 2007
Posts: 23
Reputation: poonams is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
poonams's Avatar
poonams poonams is offline Offline
Newbie Poster

Re: numeric TextBox

  #4  
Dec 6th, 2007
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))
Last edited by cscgal : Dec 6th, 2007 at 2:30 pm. Reason: Added code tags
Reply With Quote  
Join Date: Nov 2007
Posts: 63
Reputation: Sawamura is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
Sawamura's Avatar
Sawamura Sawamura is offline Offline
Junior Poster in Training

Re: numeric TextBox

  #5  
Dec 6th, 2007
i think you can see ascii table and find the ascii character as you want.
Ok.
all for the best
Last edited by Sawamura : Dec 6th, 2007 at 2:10 pm.
Reply With Quote  
Join Date: Dec 2007
Posts: 3
Reputation: chewmp is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
chewmp chewmp is offline Offline
Newbie Poster

Re: numeric TextBox

  #6  
Dec 6th, 2007
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
Reply With Quote  
Join Date: Feb 2007
Location: Bangalore,India
Posts: 1,444
Reputation: debasisdas is on a distinguished road 
Rep Power: 4
Solved Threads: 87
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Nearly a Posting Virtuoso

Re: numeric TextBox

  #7  
Dec 11th, 2007
You just need to handle the KeyChar
Share your Knowledge.
Reply With Quote  
Join Date: Mar 2008
Posts: 3
Reputation: nasren7585 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nasren7585 nasren7585 is offline Offline
Newbie Poster

Re: numeric TextBox

  #8  
Apr 16th, 2008
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
Reply With Quote  
Join Date: Mar 2008
Posts: 3
Reputation: nasren7585 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nasren7585 nasren7585 is offline Offline
Newbie Poster

Re: numeric TextBox

  #9  
Apr 16th, 2008
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
Reply With Quote  
Join Date: Nov 2006
Posts: 743
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 100
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: numeric TextBox

  #10  
Apr 16th, 2008
Hi,

Try This :

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


Regards
Veena
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 9:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC