Capturing the Enter / Backspace key and only allowing numbers

I have a form that has 1 text box so a barcode scanner can input a number or the user can type it manually.

i got most of the following code from a previous EE post:

    Private Sub txtBarcode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBarcode.KeyPress

        If Not ((Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 8) Then
            e.Handled = True
        End If

    End Sub

This works for validating that numbers are only pushed but it doesnt allow backspace or enter to work. How would i add these in?

I am new to VB.NET so if someone could break down this code and tell me whats happening it would be very handy. Also, is there a list of KeyChars or soemthing anywhere?

I also have an OK button (for manual entry) that sets the input to a public integer and closes the form and opens another one.

As the last input from the barcode scanner is an enter key, i want it so if enter is pushed in the text box, it pushes the ok button automatically.


While i think of it also, (a totally seperate issue) I have another form with multiple text boxes. How can I use the Enter key to be like the Tab key and advance to the next item?
LVL 1
bryanfordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

appariCommented:
to allow back space

    Private Sub txtBarcode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBarcode.KeyPress

        If Not ((Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 8 Or e.KeyChar = ControlChars.Back) Then
            e.Handled = True
        End If

    End Sub
0
appariCommented:
>>As the last input from the barcode scanner is an enter key, i want it so if enter is pushed in the text box, it pushes the ok button automatically.

in form properties set acceptbutton property to the default button you want to click
0
bryanfordAuthor Commented:
thanks for that

however your solution for backspace doesnt work :(

is there a type-o?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

appariCommented:

no same code working here, are you getting any error?
0
SanclerCommented:
As

   Asc(e.KeyChar) = 8

is, or should be, equivalent to

   e.KeyChar = ControlChars.Back

the original code should have worked so far as backspace is concerned.  If neither work, there's something else going on somewhere.

Is the form's .KeyPreview set to True and, if so, do you have any code responding to that?

A listing of ControlChars is here

http://msdn2.microsoft.com/en-us/library/f63200h0.aspx

A listing of ASCII values, which indicates (by mnemonics in the "code" column) how some of them translate to control characters, is here

http://msdn2.microsoft.com/en-us/library/60ecse8t(en-US,VS.80).aspx

Roger
0
planoczCommented:
Hi  bryanford,

"As the last input from the barcode scanner is an enter key, i want it so if enter is pushed in the text box, it pushes the ok button automatically."

Here is an example with keypress...

  Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim KeyAscii As Short = Asc(e.KeyChar)
        Select Case KeyAscii
            Case System.Windows.Forms.Keys.Back  '<--- this is for  backspace
            Case 13          '<---- Enter key
                e.Handled = True
                SendKeys.Send("{TAB}")   '<---- use to tab to next textbox or control
                SendKeys.Send("{Enter}")   '<---- use to Enter or execute the next control
                KeyAscii = 0
            Case 27           '<----- Escape key
                Me.Close()
                e.Handled = True
            Case Is <= 32
                ' KeyAscii = 0
            Case 48 To 57     '<--- this is for numbers
                Exit Sub
            Case 65 To 90     '<--- this is for Uppercase Alpha
                Exit Sub
            Case 97 To 122     '<--- this is for Lowercase Alpha
                Exit Sub
            Case Else
                e.Handled = True
        End Select
    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bryanfordAuthor Commented:
i am still interested in trying this out however my laptop with visual studio on it is in for service and will be for another week or so. I wont be able to test out solutions until i get it back.
0
bryanfordAuthor Commented:
using planocz code, it seems to work. Thanks, sorry for delay
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.