VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,492 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,826 people online right now. Registration is fast and FREE... Join Now!





Allow Numeric only procedure just 5 line

This procedure will allow only number with one"." one "-" and one "+" also

Submitted By: thava
Actions:
Rating:
Views: 1,388

Language: VB.NET

Last Modified: October 21, 2008
Instructions: you just copy this procedure in a module and you call this procedure for any form and for any textbox call this procedure in the keypress
like this

Private Sub TxtBalance_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtBalance.KeyPress
e.Handled = IsNonNumeric(sender, e)
End Sub

the textbox might be in any form

Snippet


  1. 'since this procedure is call on any form  we get controls sender object
  2. Public Function IsNonNumeric(ByVal cont As Object, ByVal e As KeyPressEventArgs) As Boolean
  3. 'convert the cont variable from object type to control type for get the text that is currently present in the control(textbox)
  4. 'why Should we need this? Read the Explanation
  5. 'for example 34 is already exists now the user enter a non numeric key (a for example) then it is checked like this
  6. 'isnumeric(34a) then its clearly false
  7. 'you have a little doubt why i add the "0" in last the reason i add this when the textbox is empty the user want to enter the
  8. 'negative value so the user press the "-" but it is not a numeric so when we add like this
  9. '"-0" is a numeric and there after it doesn't enter the "-" also
  10. 'the same reason for the "." also
  11. ' note that ".."or"--"or"++" is also not allowed (i.e.) more that one "." or "-" or "+" is not allowed
  12.         If IsNumeric(CType(cont, Control).Text + e.KeyChar + "0") = False Then
  13. 'a Non numeric character is entered here check whether it's not the back space
  14.             If e.KeyChar <> Chr(Keys.Back) Then
  15. 'if not backspace then return true
  16.                 IsNonNumeric = True
  17.             End If
  18.         End If
  19. ' if the function returns true then the user preseed a nonNumeric Key and it's handled
  20. End Function
  21.  
  22.  
  23.  
  24.  
  25.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month