Solved

Chr(Key) - Numeric Keypad

Posted on 2006-11-21
8
1,159 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
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 168 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 166 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 166 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 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