Windows Dialog does not handle keydown event Enter or Return

K

kgerritsen

I am building an application that will receive input from a barcode
scanner. The barcode scanner is configured to append to the front
value a single character and hyphen that identify the barcode
standard, then the value in the barcode, terminated by a return. The
barcode scanner appears to the operating system as a USB keyboard.

I am loading the barcode value into a textbox both on my Form1 and on
a dialog box where the user has to make a decision and scan a keyword
barcode to indicate their decision.

The following code does exactly what I want on Form1, but never sees
key values for Enter or Return on the dialog. I am calling the dialog
with ShowDialog (I want form1 to halt until the user makes this
decision).

Even when debugging, I can pound on the enter key on the regular
keyboard, and the keydown is never accepted by Dialog1. Is there some
special property of Enter that it always goes to form1 or a parent of
a dialog?

As a workaround, I am using the TextChanged event and looking for the
exact strings. This sends the remaining Enter keydown from the
barcode scan back to Form1, which I'm attempting to trap, but it's a
kludge, and makes for slightly different behavior whether I have the
barcode scanner on or am doing a keyboard based demo.

I saw one other post about a slightly different issue, but similar in
that an Enter key value, ostensibly for the second form, was flowing
back to the first form.

Any advice?

Thanks,
Keith

Private Sub TextBox1_Keydown(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then
Label2.Text = "Scan value: " + TextBox1.Text
If UCase(Trim(TextBox1.Text)) = "W-SENDQC" Then
Me.DialogResult =
System.Windows.Forms.DialogResult.Abort
Me.Close()
ElseIf UCase(Trim(TextBox1.Text)) = "W-OK" Then
Me.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Close()
Else
Label2.Text = "Scan value: " + TextBox1.Text + " is
not OK or QC!"

End If
Else
Label2.Text = "Scan value: " + TextBox1.Text + " is not OK
or QC!"
End If
End Sub
 
D

Duracel

Does one of your buttons have a dialog result, i.e "OK"? Windoze will
preview the key and automatically "OK" your form on return or enter if it
does. Same with cancel and escape.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top