We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Capturing the Enter / Backspace key and only allowing numbers

bryanford
bryanford asked
on
Medium Priority
938 Views
Last Modified: 2010-08-05
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?
Comment
Watch Question

Commented:
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
Commented:
>>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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
thanks for that

however your solution for backspace doesnt work :(

is there a type-o?

Commented:

no same code working here, are you getting any error?
Commented:
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
CERTIFIED EXPERT
Commented:
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

Author

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.

Author

Commented:
using planocz code, it seems to work. Thanks, sorry for delay
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.