Processing numeric keys (D0 - D9 and NumPad0 - NumPad9)

  • Thread starter Thread starter Dino Buljubasic
  • Start date Start date
D

Dino Buljubasic

Hi,

Can somebody help me with this problem please.

I need to get the value, not just to check for it, of a number
pressed but it has to work for both, numbers above QUERTY and numbers
entered from numeric pad.

I know how to check for the keys (which I am doing in the code below).
What I need is the value (e.g if D2 or NumPad2 then ' 2') so I can
then select the number 2 in my combo box (represents months).

I have several combo boxes on the form, some of which are dealing with
Strings (e.g. names of countries) which I am processing just fine, and
the others dealing with numbers (e.g. months). I process these fine
as well but only for numbers above QUERTY. What I need is the way to
do it for numeric keypad, too.

Here is the code I am using so far:

Private Sub WcboMonth_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles WcboMonth.KeyUp

If ((e.KeyCode >= Keys.D1) And (e.KeyCode <= Keys.D9)) Then

// ^^^^^ THIS WORKS FINE ^^^^^^^

Dim strString As String = ChrW(e.KeyData)
Dim index As Integer = 0

Dim i As Integer = 0
For i = 0 To WcboMonth.Items.Count - 1
If (WcboMonth.Items(i).Text.Chars(0) = strString) Then
index = i
Exit For
End If
Next

WcboMonth.SelectedIndex = index
End If


If ((e.KeyCode >= Keys.NumPad1) And _
(e.KeyCode <= Keys.NumPad9)) Then

// ^^^^^THIS DOES NOT WORK ^^^^^^

// (it returns 'e' if NumPad5 clicked
Dim strString As String = ChrW(e.KeyData)
Dim index As Integer = 0

Dim i As Integer = 0
For i = 0 To WcboMonth.Items.Count - 1
If (WcboMonth.Items(i).Text.Chars(0) = _
strString) Then
index = i
Exit For
End If
Next

WcboMonth.SelectedIndex = index
End If
End Sub

Any help will be appreciated

thank you
_dino_
 
I find the way how to do this. For those interested, I used wrong
event handler. I used KeyUp instead of using KeyPress (... I know,
one of these things ...)

Anyways, use KeyPress instead of KeyUp or so and then you have
e.KeyChar available which will return you the character pressed no
matter wehter you are using numeric pad, or numbers above QUERTY.

_dino_
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top