Link to home
Start Free TrialLog in
Avatar of bryanford
bryanford

asked on

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?
Avatar of appari
appari
Flag of India image

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
SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bryanford
bryanford

ASKER

thanks for that

however your solution for backspace doesnt work :(

is there a type-o?

no same code working here, are you getting any error?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
using planocz code, it seems to work. Thanks, sorry for delay