Solved

Chr(Key) - Numeric Keypad

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
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…

691 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