problem with keyboard keys and their corresponding codes

jalo

Freshman
Joined
Aug 9, 2005
Messages
28
i am trying to enable specific key presses. i am having trouble with the period key and the decimal key. i tried this

Code:
Asc(e.KeyChar) = Keys.Decimal
Asc(e.KeyChar) = Keys.OemPeriod

but it doesn't seem to work... the first line does not respond to any key press, while the second line responds to pressing "n" lowercase... i am puzzled... anyone have an idea?
 
I don't understand what you are trying to do - it looks like you're trying to set the key that was was pressed from the event args - which you can't do. If you're simply trying to test if the 'period' was pressed you should:

C#:
if(Convert.ToInt32(e.KeyChar)==Convert.ToInt32('.'))
{
      //Do whatever
}

Visual Basic:
If Convert.ToInt32(e.KeyChar) = Convert.ToInt32("."c) Then
        'Do something
End If
 
i was trying to check whether a keypress is a period (in order to allow that keypress). thanks for the tip - works for me! :-)

Just wodering about the syntax though - could you explain the ("."c) - i am puzzled by the c....

Visual Basic:
If Convert.ToInt32(e.KeyChar) = Convert.[COLOR=Red]ToInt32("."c)[/COLOR] Then
        'Do something
End If
[/QUOTE]
 
The c indicates that the value is a Char constant. A Char is a value type that holds a single character (sort of like a 1-character fixed length string). Just like the "r" in 1.0r indicates that a value is a double, the "c" in "."c indicates that the value is a Char (as opposed to a string).
 
Back
Top