Check To See If Decimal Key Was Pressed

Hi Experts,
I have a function in a module that will check to see if the user has pressed a number key or a some other keys. It seems to work but it will not see the user pressing the decimal key. I call this from the keypress event from a textbox and pass the KeyPressEventArgs.

This is the function





Thank You
John

Public Function CheckNumberKeypress(ByVal Input As KeyPressEventArgs) As Boolean
 
 
CheckNumberKeypress = False
 
If IsNumeric(Input.KeyChar) = True Then CheckNumberKeypress = True
 
If Input.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Back) Then CheckNumberKeypress = True
 
If Input.KeyChar = Microsoft.VisualBasic.ChrW(Keys.CapsLock) Then CheckNumberKeypress = True
 
If Input.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Decimal) Then CheckNumberKeypress = True
 
If Input.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then CheckNumberKeypress = True
 
 
End Function
 
 
 
This is The Call from the form:
 
    Private Sub txtLinearFeet_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLinearFeet.KeyPress
 
        If CheckNumberKeypress(e) = False Then
            e.KeyChar = Nothing
        End If
 
    End Sub

Open in new window

DjJohnnyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi DjJohnny;

There is no 1 to 1 relationship between Keys enumeration and and its value with CharW. In the case of Keys.Decimal has a numeric value of 110 which translate to 'n' when ChrW(Keys.Decimal) is applied.

See snippet for a solution.

Fernando
Public Function CheckNumberKeypress(ByVal Input As KeyPressEventArgs) As Boolean
 
    CheckNumberKeypress = False
    If IsNumeric(Input.KeyChar) = True Then CheckNumberKeypress = True
    If Input.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Back) Then CheckNumberKeypress = True
    If Input.KeyChar = Microsoft.VisualBasic.ChrW(Keys.CapsLock) Then CheckNumberKeypress = True
    If Input.KeyChar = "."c Then CheckNumberKeypress = True
    If Input.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then CheckNumberKeypress = True
 
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
drichardsCommented:
The Decimal is probably the keypad '.' key when the keypad is in a certain mode.

You're probably wanting key code 0x2e which is the ASCII value of '.'.  The Keys value for this is Keys.Delete, which you probably shouldn't use.  I am not sure exactly what Keys is.
0
DjJohnnyAuthor Commented:
FernandoSoto

Thank You. That worked like a charm.

John
0
Fernando SotoRetiredCommented:
Not a problem John, glad I was able to help.  ;=)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.