Solved

Chr(Key) - Numeric Keypad

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now