5 Replies - 2754 Views - Last Post: 04 June 2010 - 05:30 PM

#1 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Allow Numeric only TextBox procedure just 5 line

Posted 17 January 2009 - 06:04 PM

Description: 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 KeyAscii = NumericOnly(control, keyascii)This procedure will allow only number with one"." one "-" and one "+"
Public Function NumericOnly(ByVal cont As Object, ByVal KeyAscii As integer) As Boolean
on Error goto ER:
'convert the cont variable from object type to TextBox 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, TextBox).Text + chr(KeyAscii) + "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
                NumericOnly = 0
            End If
        End If
' if the function returns true then the user preseed a nonNumeric Key and it's handled
ER:
NumericOnly = KeyAscii
End Function



Is This A Good Question/Topic? 0
  • +

Replies To: Allow Numeric only TextBox procedure just 5 line

#2 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: Allow Numeric only TextBox procedure just 5 line

Posted 17 January 2009 - 06:04 PM

Description: 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 KeyAscii = NumericOnly(control, keyascii)This procedure will allow only number with one"." one "-" and one "+"
Public Function NumericOnly(ByVal cont As Object, ByVal KeyAscii As integer) As Boolean
on Error goto ER:
'convert the cont variable from object type to TextBox 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, TextBox).Text + chr(KeyAscii) + "0") = False Then
'a Non numeric character is entered here check whether it's not the back space 
            If keyascii <> 8 Then
'if not backspace then return true
                NumericOnly = 0
            End If
        End If
' if the function returns true then the user preseed a nonNumeric Key and it's handled
ER:
NumericOnly = KeyAscii
End Function


Was This Post Helpful? 0
  • +
  • -

#3 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: Allow Numeric only TextBox procedure just 5 line

Posted 17 January 2009 - 06:04 PM

Description: 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 KeyAscii = NumericOnly(control, keyascii)This procedure will allow only number with one"." one "-" and one "+"
Public Function NumericOnly(ByVal cont As Object, ByVal KeyAscii As integer) As Integer
on Error goto ER:
'convert the cont variable from object type to TextBox 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, TextBox).Text + chr(KeyAscii) + "0") = False Then
'a Non numeric character is entered here check whether it's not the back space 
            If keyascii <> 8 Then
'if not backspace then return true
                NumericOnly = 0
            End If
        End If
' if the function returns true then the user preseed a nonNumeric Key and it's handled
ER:
NumericOnly = KeyAscii
End Function


Was This Post Helpful? 0
  • +
  • -

#4 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: Allow Numeric only TextBox procedure just 5 line

Posted 17 January 2009 - 06:04 PM

Description: 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 Text1_KeyPress(KeyAscii As Integer) KeyAscii = NumericOnly(Text1, KeyAscii) End SubThis procedure will allow only number with one"." one "-" and one "+"
Public Function NumericOnly(ByVal cont As Object, ByVal KeyAscii As Integer) As Integer
'convert the cont variable from object type to TextBox 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(cont.Text + Chr(KeyAscii) + "0") = False And KeyAscii <> 8 Then
            'a Non numeric character is entered here check whether it's not the back space
            'if not backspace then return true
            NumericOnly = 0
        Else
            NumericOnly = KeyAscii
        End If
' if the function returns true then the user preseed a nonNumeric Key and it's handled
End Function

Was This Post Helpful? 0
  • +
  • -

#5 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: Allow Numeric only TextBox procedure just 5 line

Posted 17 January 2009 - 06:04 PM

Description: 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 Text1_KeyPress(KeyAscii As Integer) KeyAscii = NumericOnly(Text1, KeyAscii) End SubThis procedure will allow only number with one"." one "-" and one "+"
Public Function NumericOnly(ByVal cont As Object, ByVal KeyAscii As Integer) As Integer
'convert the cont variable from object type to TextBox 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(cont.Text + Chr(KeyAscii) + "0") = False Or UCase(Chr(KeyAscii)) = "D") And KeyAscii <> 8 Then
            'a Non numeric character is entered here check whether it's not the back space
            'if not backspace then return true
            NumericOnly = 0
        Else
            NumericOnly = KeyAscii
        End If
' if the function returns true then the user preseed a nonNumeric Key and it's handled
End Function

Was This Post Helpful? 0
  • +
  • -

#6 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: Allow Numeric only TextBox procedure just 5 line

Posted 04 August 2009 - 06:03 PM

sorry to every one, now i have change my snippet a little bit now use this code recently i modified the post with on error statement that cause the problem now i changed some thing rather than this with out an on error statement
Was This Post Helpful? 0
  • +
  • -

#7 ynnhoj67   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-July 07

Re: Allow Numeric only TextBox procedure just 5 line

Posted 04 August 2009 - 06:44 PM

i don't see any difference yet...error says ctype not defined
Was This Post Helpful? 0
  • +
  • -

#8 green_roses_5451   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-August 09

Re: Allow Numeric only TextBox procedure just 5 line

Posted 19 September 2009 - 07:39 AM

Actually sometimes it accepts one chr d
Was This Post Helpful? 0
  • +
  • -

#9 aakkaarr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 04-June 10

Re: Allow Numeric only TextBox procedure just 5 line

Posted 04 June 2010 - 12:59 PM

I agree with green_roses
Was This Post Helpful? 0
  • +
  • -

#10 thava   User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,607
  • Joined: 17-April 07

Re: Allow Numeric only TextBox procedure just 5 line

Posted 04 June 2010 - 05:30 PM

God, It's True, i didn't know why vb allow this string "00d0" as a numeric now i changed it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1