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?
 
Fernando SotoConnect With a Mentor RetiredCommented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.