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 (); } } };