Solved

Chr(Key) - Numeric Keypad

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

8 Experts available now in Live!

Get 1:1 Help Now