Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,257 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,297 people online right now. Registration is fast and FREE... Join Now!




Convert.ToChar and number pad

 
Reply to this topicStart new topic

Convert.ToChar and number pad, unexpected results when trying to convert numberpad keys

Locuranis
8 Jul, 2008 - 09:47 AM
Post #1

New D.I.C Head
*

Joined: 14 Jun, 2008
Posts: 9

Hello. I'm currently working on a calculator, (yes, one of those again), and I have it functioning for the most part. Now I'm trying to allow for keyboard input, but when I use Convert.ToChar with numberpad keys, it displays letters instead. I know if I subtract 48 from the keycode value I'll get the corresponding number, but just wondering if there's another way to go about it.

CODE
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles Me.KeyDown

    Select Case e.KeyCode
        Case Keys.D0 To Keys.D9, Keys.NumPad0 To Keys.NumPad9
            btnNumbers(Convert.ToChar(e.KeyCode))
    End Select

End Sub


I know that's just one Case condition there. I have some others planned, but I wanted to make this work first. Any help appreciated. Thank you.

This post has been edited by Locuranis: 8 Jul, 2008 - 09:49 AM

User is offlineProfile CardPM
+Quote Post


modi123_1
RE: Convert.ToChar And Number Pad
8 Jul, 2008 - 10:07 AM
Post #2

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,216



Thanked: 49 times
Dream Kudos: 150
My Contributions
QUOTE(Locuranis @ 8 Jul, 2008 - 12:47 PM) *

Hello. I'm currently working on a calculator, (yes, one of those again), and I have it functioning for the most part. Now I'm trying to allow for keyboard input, but when I use Convert.ToChar with numberpad keys, it displays letters instead. I know if I subtract 48 from the keycode value I'll get the corresponding number, but just wondering if there's another way to go about it.

CODE
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles Me.KeyDown

    Select Case e.KeyCode
        Case Keys.D0 To Keys.D9, Keys.NumPad0 To Keys.NumPad9
            btnNumbers(Convert.ToChar(e.KeyCode))
    End Select

End Sub


I know that's just one Case condition there. I have some others planned, but I wanted to make this work first. Any help appreciated. Thank you.


why are you trying to convert from the enumeration? keep it in the enumeration! (you can compare it against specific enumerations at any time)
User is online!Profile CardPM
+Quote Post

Locuranis
RE: Convert.ToChar And Number Pad
8 Jul, 2008 - 10:35 AM
Post #3

New D.I.C Head
*

Joined: 14 Jun, 2008
Posts: 9

Because I'm trying to pass the number character as a string, which is then handled in the function. Passing the keycode itself isn't what I want to do.

I'm not comparing anything. If a user hits a number either at the top or on the keypad, it should, after some flag checking, display the number in the output. The above code works okay in doing that with the numbers keys at the top, but not in the number pad.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Convert.ToChar And Number Pad
8 Jul, 2008 - 11:34 AM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You could use the KeyPress event to capture the keystrokes. Then you can use the KeyChar to get the character representation of the numbers.

CODE

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar >= "0" And e.KeyChar <= "9" Then
            btnNumbers(e.KeyChar)
        End If
    End Sub

User is offlineProfile CardPM
+Quote Post

Locuranis
RE: Convert.ToChar And Number Pad
8 Jul, 2008 - 11:46 AM
Post #5

New D.I.C Head
*

Joined: 14 Jun, 2008
Posts: 9

QUOTE(jayman9 @ 8 Jul, 2008 - 12:34 PM) *
You could use the KeyPress event to capture the keystrokes. Then you can use the KeyChar to get the character representation of the numbers.


Thank you, this managed to work and cut down on code. Originally I had a select case for each key, so that nixes about 10+ lines of code to just three. Thanks again!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 11:04AM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month