WinForm in the textbox to determine the scanner input and keyboard input

Source: Internet
Author: User

This article reproduced: http://www.cnblogs.com/Hdsome/archive/2011/10/28/2227712.html

Ask the question: in the receiving system, often use scanning gun scanning barcode input to the textbox, when the barcode can not be scanned, you need to manually input. If the scanner is input, we will automatically interpret the bar code, and manual input, the final need to add press ENTER to confirm the interpretation of bar code. At this point we will determine whether the input device is a manual or a scanning gun.

Try the method:

1. Set the TextBox property to Readonly=true. Result: Could not be entered.

2. Set the property e.handle=true in the KeyPress event of the TextBox. Result: The KeyPress event is also triggered when the scanner is entered, so it cannot be entered.

3. Judge the result in the ValueChanged event of the TextBox. Result: The scanner is also a character input, not one-time to the entire barcode input.

Thinking: The scanner actually looks exactly like the keyboard on the input. However, the difference between manual input and scan device input is that the scanning device input speed is relatively fast and the time interval is more average.

Experiment:

The experimental result proves the beginning inference.

Workaround:

Private DateTime _dt = DateTime.Now; Define a member function to hold a point in time for each
private void Textbox1_keypress (object sender, KeyPressEventArgs e)
{
DateTime TEMPDT = DateTime.Now; Save the button at the point in time
TimeSpan ts = TEMPDT.     Subtract (_DT); Get time interval
if (Ts.milliseconds > 50)//judgment interval, if the time interval is greater than 50 milliseconds, the TextBox will be emptied
TextBox1.Text = "";
DT = TEMPDT;
}

At this point, the problem solved, I hope we have a better way to exchange messages

This article is used in the actual project;

DateTime Dtstart = DateTime.Now;                this.txtCustomerNo.TbKeyPress + = (sender, E) = = {DateTime dtcurrent = DateTime.Now;                Console.WriteLine ("Dtstart:" + dtstart.tostring ());                Console.WriteLine ("Dtcurrent:" + dtcurrent.tostring ());                TimeSpan ts = dtcurrent.subtract (Dtstart);                    if (Ts.milliseconds <) {Isscanninggunauto = true;                Console.WriteLine ("Scan gun; TS:" + ts.Milliseconds.ToString () + "Text:" + This.txtCustomerNo.Text.Trim ());                    } else {Isscanninggunauto = false;                Console.WriteLine ("Manual input; TS:" + ts.Milliseconds.ToString () + "Text:" + This.txtCustomerNo.Text.Trim ());                } Dtstart = Dtcurrent;            Console.WriteLine ("----------------------------------------");            }; This.txtCustomerNo.TbKeyDown + = (sender, e) + = {if (this.txtCustomerNo.TbFocused) {if (E.keycode = = Keys.enter) {if (Isscanninggunauto) Scanningguna                        Uto ();                        else//btnseach_click (null, NULL);                    This.txtCustomerName.Focus (); }                }            };

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.