如何在我的C#应用程序中读取背景中的条码文本?我google搜索,但它没用。和其他资源在stackoverflow不接近我所需要的。我想在后台读取条形码。我想知道数据是来自条形码还是键盘。如果数据来自条形码,则即使文本框突出显示,它也不能显示在文本框上。我得到了类似的代码,计算器,但如果有窗口文本的存在,则文本框将包含条码数据;我不想要。 链接:get barcode reader value form background monitoringC#+ USB条码阅读器
DateTime _lastKeystroke = new DateTime(0);
List<char> _barcode = new List<char>(10);
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
// check timing (keystrokes within 100 ms)
TimeSpan elapsed = (DateTime.Now - _lastKeystroke);
if (elapsed.TotalMilliseconds > 100)
_barcode.Clear();
// record keystroke & timestamp
_barcode.Add(e.KeyChar);
_lastKeystroke = DateTime.Now;
// process barcode
if (e.KeyChar == 13 && _barcode.Count > 0) {
string msg = new String(_barcode.ToArray());
MessageBox.Show(msg);
_barcode.Clear();
}
}
而如果我在我的应用程序的多个文本框后按下。我需要在同一窗口中记录条形码数据以及文本框数据。你可能会认为我的申请像POS一样。 – Redone
在这种情况下,你通常有一个计时器,如果说在x秒内没有用户活动(即打字)然后强制焦点回到条形码文本框或有一个按钮,操作员点击叫** **扫描弹出窗体说“请立即扫描...”,其中包含隐藏的文本框。这取决于你当然的用户体验。 – Belogix
@Belogix为什么不只是为WinForm Textbox设置[Control.Visible](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.visible.aspx)而不是移动它?这样你就可以拥有一个绑定到文本框可见性的属性。 –