2

My program is already working fine, I use a TextBox to capture the barcode scanner input. The purpose of my program is for time and attendance monitoring, the problem is I want to prevent users from using the keyboard to type in their ID's as it would render the barcode scanner and their ID's with barcodes useless.

*I already tried removing the keyboard from the computer and it did work, but the keyboard must not be removed as a requirement...

4
  • Question is not clear that what exactly you want. are you displaying barcode reader result into a textbox and wants users to restrict from typing in manually? Commented Jan 3, 2013 at 7:59
  • Yes, I want to restrict users from typing in manually. Is it possible?... Commented Jan 3, 2013 at 8:08
  • Just set the textbox ReadOnly property to true Commented Jan 3, 2013 at 8:11
  • Already did that...it does prevent typing from the keyboard but also prevents barcode inputs... Commented Jan 3, 2013 at 8:14

12 Answers 12

8

Option 1:

Get a barcode-scanner that is connected to a serial-port (raw serial device read by a COM port). As most barcode-scanners emulate keyboard strokes there is no way to directly distinguish a barcode scanner input from a keyboard input (see next option) without going low-level (see last update).

One connected to a serial port (or emulated one via USB as serial-ports are not so common anymore) gives you full control on where the input comes from.

Option 2:

Count number of chars typed by time. Barcode-scanners inject a sequence (line) pretty fast compared to typing. Measuring the time used in the textbox by counting key-presses (use CR+LF as a measure point as these are sent by the scanner as well) can give you one method to distinguish if a human is typing (unless there is one typing fast as f) or the content was injected. If timed-out just reject/clear the input.

In addition the checksum of the barcode (if you use one that contains that) can be used to do an extra validation in addition to time measurement.

(you can detect pasting by overriding the ctrl + v as in the next option).

Option 3:

Combine option 2 but instead of measure in the textbox tap into the ProcessCmdKey() function (by overriding it) and measure there if textbox has focus. This way you can first buffer input, measure time and if within a set time-out value, inject the line into the textbox.

Update:

Option 4: a non-technical approach -

Usability improvements: make it visually very clear that bar-codes must be entered with a scanner and not typed. I am including as an option as it is simple and if made correct also effective (there's no right answer of what is correct unfortunately).

Approached could include f.ex. a watermark in the textbox ("Don't type, scan!" or something in that order). Give it a different color, border, size etc. to distinguish it from normal textboxes, and have a help text associated and available at all time that improves clarity.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Some good options here. The ideal one is to directly interface to the scanner but I really like #2 as a clever work-around!
3

I had the same issue and I did the following:

  1. I set an int variable digitsPrevTyped = 0

  2. In the "TextChanged" event of my textbox I added this (the textbox has a maxsize of 17 chars):

    Private Sub tbxScannedText_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbxScannedText.TextChanged If tbxScannedText.Text.Length >= 17 Then SearchFunction(False) Else digitsPrevTyped = tbxScannedText.Text.Length End If End Sub 
  3. Then in my "SearchFunction" I check the following:

    Dim inputMethod As Char If tbxScannedText.TextLength = 17 And digitsPrevTyped = 0 Then inputMethod = TEXT_SCANNED Else inputMethod = TEXT_MANUALLY_ENTERED End If 

If the textbox initially had a length of 0 chars and now has a length of 17 chars it means that the text was scanned. If the length of the previously typed text is less than 17 chars, then the text was typed. It is very basic but it works for me.

Comments

1

The other possible workaround is to handle keypress event to restrict user input. Do not allow direct input from keyboard and leave the readonly false.

Set following in KeyPress event handler

Private Sub Textbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress e.Handled = True End Sub 

3 Comments

I'm trying it right now, but it's not working..it is also preventing inputs from barcode scanner...
I dont know how barcode reader set the text so, you need to take the barcodereader output and set the textbox text property yourself. After handling Keypress event, it allows you to do Textbox1.Text="some new text" from any other place. if possible to share code for barcode reading then it will be easy to figure out some workaround
This won't work. The reason is that a Keyboard Barcode basically simulates keystrokes from the keyboard. Meaning that there is no way one can differentiate the method for input. I think the best option is to get a barcode for COM part (or USB simulating COM). The other method of timing the rate of entry has it's flaw too. Since you still have the keyboard; user can open a Notepad, type in the UserID into the Notepad, copy and paste into the textbox. and since it is COPY/PASTE, it will beat the keystroke rate.
1

Just disable the keyboard anyway.. when using barcode you can disable the keyboard without using readonly on the textbox..

on keypress event put some code i.e

if e.keychar <> chrw(0) then e.keychar = chrw(0) end if

that condition will automatically be trigged when user type anything.. you will forcibly disable any input from user but not from barcode

Comments

1

why not use an "alias" in the bar code like "123@#$!" (but make it stupid long) is "JSMITH" and set the font color to the same as the background color in the textbox. The user can't see what they're typing or what the bar code value is when it's scanned.

Super simplistic approach that doesn't really require anything added aside from another field in the the user table.

1 Comment

heck make the texbox border the same as the background as well at it will ALMOST be like the textbox doesn't exist.
0

This is an old post, but it took me some time to figure out a relatively clean way to use a barcode scanner and combobox so this is for future users.

Barcode scanners can often be configured to append carriage return and line feed to the end of the scan. I have a form that can take user input or barcode scanner input into a bound combobox using the _PreviewKeyDown property and trapping on the value "Keys.Enter".

Example:

If ((e.KeyCode = Keys.Enter) Then 'do stuff Else 'do other stuff End if 

Verifying the data exists in the datasource is a bit trickier because the SelectedValue property of the combobox doesn't update so that event doesn't fire. I used a custom method to verify that the value scanned exists in the datasource. This method uses the .Text property of the combo box. It uses:

Me.combobox.findexactstring(Me.combobox.Text) 

Comments

0
If e.KeyCode = Keys.Enter And txt.Text.Length > 0 Then 'To Do Else 'To Do End if 

1 Comment

If Asc(eventArgs.KeyChar) = 13 And txt.Text.Length > 0 Then
0

All of my scanner input goes into a "hidden" textbox, which then fills the visible ones as needed depending on the input. This, of course, means you need to keep track of where the focus is. Any type of control that can get focus will then make a call in those events to return focus to whatever the "active" textbox is at that time, which is normally the hidden one. For example...

Private Sub buttons_gotFocus(sender As System.Object, e As System.EventArgs) Handles btnPrint.GotFocus, btnInMVPageDown.GotFocus, btnAdv.GotFocus, btnManual.GotFocus, btnResend.GotFocus, dgvInbound.GotFocus, dgvOutbound.GotFocus, TCRole.GotFocus Try activeTextbox.Focus() Catch ex As Exception 'ignore any errors End Try End Sub 

Most other textboxes are disabled by default, and only enabled under certain conditions. Once that entry is done they are disabled and the hidden one will get focus again. Works like a charm.

Comments

0

There's no need to record previous typed characters.

Here's my solution:

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged If TextBox1.Text.Length >= 17 Then '17 or the number of characters your scanner gets. MsgBox("scanned") TextBox1.Clear() Else If TextBox1.Text.Length <> 0 Then TextBox1.Clear() End If End Sub 

Comments

0

This answer will handle any fast typing.

Dim scanner_input As Boolean = False Dim start_typing As DateTime Private Sub TextBox_part_number_TextChanged(sender As Object, e As EventArgs) Handles TextBox_part_number.TextChanged If (TextBox_part_number.Text.Length = 1) Then start_typing = DateTime.Now scanner_input = False '' MsgBox(start_typing.ToString) ElseIf (TextBox_part_number.Text.Length > 7) Then If (calc_typing_time(start_typing) < 500) Then scanner_input = True Else scanner_input = False End If End If End Sub Function calc_typing_time(time_started As DateTime) Dim time_finished As DateTime time_finished = DateTime.Now Dim duration As TimeSpan = time_finished - time_started Dim time_diff As String = duration.TotalMilliseconds Return time_diff End Function 

Comments

0

Most of the scanners has a driver to communicate with (Opos) it has functions to open the scanner port and listen to the scanning , so you take the result and decode it in the background and then display the result in the Textbox... what you need to do it to check your barcode scanner's brand go to it's website and download the driver and its manual.

Comments

-2

You should just mark your textbox as readonly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.