Capturing the Enter / Backspace key and only allowing numbers
Posted on 2006-03-28
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
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?