?
Solved

Capturing the Enter / Backspace key and only allowing numbers

Posted on 2006-03-28
9
Medium Priority
?
910 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?
0
Comment
Question by:bryanford
8 Comments
 
LVL 39

Expert Comment

by:appari
ID: 16308706
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
 
LVL 39

Assisted Solution

by:appari
appari earned 160 total points
ID: 16308729
>>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
 
LVL 1

Author Comment

by:bryanford
ID: 16308745
thanks for that

however your solution for backspace doesnt work :(

is there a type-o?
0
Independent Software Vendors: 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!

 
LVL 39

Expert Comment

by:appari
ID: 16309338

no same code working here, are you getting any error?
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 180 total points
ID: 16309561
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
 
LVL 27

Accepted Solution

by:
planocz earned 160 total points
ID: 16318611
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
 
LVL 1

Author Comment

by:bryanford
ID: 16530977
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
 
LVL 1

Author Comment

by:bryanford
ID: 16643618
using planocz code, it seems to work. Thanks, sorry for delay
0

Featured Post

Industry Leaders: 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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

621 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