What's Here?
- Members: 300,492
- Replies: 826,148
- Topics: 137,485
- Snippets: 4,420
- Tutorials: 1,148
- Total Online: 1,826
- Members: 78
- Guests: 1,748
|
This procedure will allow only number with one"." one "-" and one "+" also
|
Submitted By: thava
|
|
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
'since this procedure is call on any form we get controls sender object
Public Function IsNonNumeric(ByVal cont As Object, ByVal e As KeyPressEventArgs) As Boolean
'convert the cont variable from object type to control type for get the text that is currently present in the control(textbox)
'why Should we need this? Read the Explanation
'for example 34 is already exists now the user enter a non numeric key (a for example) then it is checked like this
'isnumeric(34a) then its clearly false
'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
'negative value so the user press the "-" but it is not a numeric so when we add like this
'"-0" is a numeric and there after it doesn't enter the "-" also
'the same reason for the "." also
' note that ".."or"--"or"++" is also not allowed (i.e.) more that one "." or "-" or "+" is not allowed
If IsNumeric(CType(cont, Control).Text + e.KeyChar + "0") = False Then
'a Non numeric character is entered here check whether it's not the back space
If e.KeyChar <> Chr(Keys.Back) Then
'if not backspace then return true
IsNonNumeric = True
End If
End If
' if the function returns true then the user preseed a nonNumeric Key and it's handled
End Function
Copy & Paste
|
|
|
|