Solved

# Check To See If Decimal Key Was Pressed

Posted on 2008-11-16
701 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
``````
0
Question by:DjJohnny
4 Comments

LVL 62

Accepted Solution

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
``````
0

LVL 19

Expert Comment

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

FernandoSoto

Thank You. That worked like a charm.

John
0

LVL 62

Expert Comment

Not a problem John, glad I was able to help.  ;=)
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Title # Comments Views Activity
sql server/vb.net 15 26
Help file 1 28
vb linq Count Distinct? 1 29
MS Date Picker 64 bit 32 bit issue 12 22
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!