?
Solved

Chr(Key) - Numeric Keypad

Posted on 2006-11-21
8
Medium Priority
?
1,213 Views
Last Modified: 2008-01-09
Hi there

I have a string of allowed characters ... i.e. "0123456789ABCD"

depending on the characters in the string depends on what the user is allowed to enter into the text box.

        If Len(AcceptCharacters) > 0 Then
            If InStr(1, AcceptCharacters, Chr(Key)) = 0 Then
                FilterKeypress = 0
                Beep()
            End If
        end if

however this does not work for the numeric key pad as the characters being returned bear no resemblemence to 0123456789 (i.e I get abcd etc).  How can I do this ... I know you can do isnumeric ... but I need to be allow the user to just pass "345YH" etc, so I really need to be able to get the proper character being entered, i.e. 0123456789

Thanks!

0
Comment
Question by:DavidGreenfield
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Expert Comment

by:DEEPESH
ID: 17986276
Use keyup  event and use e.KeyValue in vent, it will return you keyascii and that will do the work for you
0
 

Author Comment

by:DavidGreenfield
ID: 17986412
sorry I should have put the code in i am calling aswell.  I am already using the keyup event and the e.keyvalue ... basically chr(e.keyvalue) is not returning 0123456789 as I want.

type 1 in numeric keypad =-> chr(e.keyvalue) = a
type 2 in numeric keypad =-> chr(e.keyvalue) = b
type 3 in numeric keypad =-> chr(e.keyvalue) = c

    Private Sub TextBox1_Keyup(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        'Validate characters being entered
        If FilterKeypress(e.KeyValue, "1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZ") = 0 Then e.SuppressKeyPress = True

    End Sub
0
 
LVL 18

Accepted Solution

by:
vbturbo earned 672 total points
ID: 17986757
Hi

A small example to validate a numeric input
Just assign the Ascii.key codes to your whatever uppercase/lowercase chars/letters to it.


    Private Sub numtxtbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles numtxtbox.KeyPress
        If KeyAscii(e) < 48 Or KeyAscii(e) > 57 Then
            If KeyAscii(e) <> 8 Then
                e.Handled = True
            End If
        End If
    End Sub

    Public Function KeyAscii(ByVal UserKeyArgument As KeyPressEventArgs) As Short
        KeyAscii = Asc(UserKeyArgument.KeyChar)
    End Function

vbturbo
0
 
LVL 17

Assisted Solution

by:ZeonFlash
ZeonFlash earned 664 total points
ID: 17986825
> I am already using the keyup event and the e.keyvalue

Actually, you were still using the KeyDown event :)  >> Private Sub TextBox1_Keyup(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

Here's another option:

      Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If FilterKeyPress(e.KeyCode.ToString.Replace("NumPad", ""), "1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZ") = 0 Then e.Handled = True
      End Sub

      Private Function FilterKeyPress(ByVal key As Object, ByVal AcceptCharacters As String) As Integer
            If Len(AcceptCharacters) > 0 Then
                  If InStr(1, AcceptCharacters, key) = 0 Then
                        Beep()
                        Return 0
                  End If
            End If
            Return 1
      End Function
0
 

Assisted Solution

by:dajoebomb
dajoebomb earned 664 total points
ID: 18038565
   Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.NumPad0
               Textbox1.text = Textbox1.text & "0" //replace here with what u want to do with the button press
            Case Keys.NumPad1
                Textbox1.text = Textbox1.text & "1"
           Case Keys.NumPad2
               Textbox1.text = Textbox1.text & "2"
            Case Keys.NumPad3
                Textbox1.text = Textbox1.text & "3"
            Case Keys.NumPad4
               Textbox1.text = Textbox1.text & "4"
            Case Keys.NumPad5
                Textbox1.text = Textbox1.text & "5"
            Case Keys.NumPad6
               Textbox1.text = Textbox1.text & "6"
            Case Keys.NumPad7
                Textbox1.text = Textbox1.text & "7"
            Case Keys.NumPad8
               Textbox1.text = Textbox1.text & "8"
            Case Keys.NumPad9
                Textbox1.text = Textbox1.text & "9"


        End Select

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function


// You might have to add more lines to deal with numbers 1,2,3,4,5,6,7,8,9,0
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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