?
Solved

Check To See If Decimal Key Was Pressed

Posted on 2008-11-16
4
Medium Priority
?
723 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:DjJohnny
  • 2
4 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 22971669
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
 
LVL 19

Expert Comment

by:drichards
ID: 22971765
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
 

Author Comment

by:DjJohnny
ID: 22971948
FernandoSoto

Thank You. That worked like a charm.

John
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22971974
Not a problem John, glad I was able to help.  ;=)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question