[英]Let user start typing when winform pops up without them having to click on the textbox
I am working on a barcode reader project in Visual Studio using C#.我正在使用 C# 在 Visual Studio 中处理条形码阅读器项目。 I have created a WinForm Application and have added a RichTextBox to it.
我创建了一个 WinForm 应用程序,并向其中添加了一个 RichTextBox。 I want the user to be able to start scanning when they open the program without having to click on the textbox.
我希望用户能够在打开程序时开始扫描,而无需单击文本框。 Thanks in advance!
提前致谢!
(I'm assuming you have an application with a multitude of stuff in it. However there is one field that needs to be filled in with a scanned barcode.) (我假设您有一个包含大量内容的应用程序。但是有一个字段需要用扫描的条形码填写。)
I faced a simular issue a while ago.不久前我遇到了一个类似的问题。 I needed to capture a barcode in WPF.
我需要在 WPF 中捕获条形码。 Setting the focus property in load seemed a good idea but because there were a multitude of other controls on the page that the user could click etc. focus jumped from one control to the other, making the barcode go in the wrong fields or vanish in a grid that has focus for example.
在 load 中设置 focus 属性似乎是一个好主意,但因为页面上有许多其他控件可供用户单击等。焦点从一个控件跳转到另一个控件,使条形码进入错误的字段或消失例如具有焦点的网格。
We were not able to use any other way of reading the barcode from the scanner because it was used for other applications too.我们无法使用任何其他方式从扫描仪读取条码,因为它也用于其他应用程序。 It had to be configured as input.
它必须配置为输入。
We came up with a solution of capturing the keypresses instead.我们想出了一个解决方案来捕获按键。 By using the keydown events we could track the scanner input and stated that if more than 5 keys came in within a limited time + with our prefix and suffix it had to be a barcode.
通过使用 keydown 事件,我们可以跟踪扫描仪输入并声明如果在有限的时间内输入了超过 5 个键 + 加上我们的前缀和后缀,它必须是条形码。
EDIT: here is a simplified version of the class.编辑:这是该类的简化版本。
public delegate void BarcodeRead(string barcode);
public class ManualReader
{
private string barcode = "no barcode detected";
private string possible = "";
private DateTime timestarted = DateTime.MinValue;
private Timer InputTimeout;
public BarcodeRead OnBarcodeRead;
public void OnKeyDown(object sender, KeyEventArgs.KeyEventArgs e)
{
//create timer if it does not exist
if (InputTimeout == null)
{
InputTimeout = new Timer(100);
InputTimeout.Enabled = true;
InputTimeout.Elapsed += new ElapsedEventHandler(OnTimedEvent);
}
//reset timer
InputTimeout.Stop();
InputTimeout.Start();
//possible barcode
possible += CharToKey.GetCharFromKey(e);
if (timestarted == DateTime.MinValue)
{
timestarted = DateTime.Now;
}
}
//Timer elapses
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
//Is it a barcode?
if ((timestarted.AddMilliseconds(600) > DateTime.Now) && (possible.Length > 5)
&& (timestarted != DateTime.MinValue) && possible.Contains("\r"))
{
barcode = possible;
barcode = barcode.Remove(0, 1);
barcode = barcode.Replace("\r", "");
//launch delegate
if (OnBarcodeRead != null)
{
OnBarcodeRead.Invoke(barcode);
}
}
//delete timers
timestarted = DateTime.MinValue;
InputTimeout.Dispose();
InputTimeout = null;
possible = null;
}
}
}
I'm aware that for really short timeouts datetime functions aren't precise but still this little 'hack' worked perfectly for our application.我知道对于非常短的超时,日期时间函数并不精确,但这个小小的“黑客”仍然对我们的应用程序完美地工作。
You can add directly in the element.您可以直接在元素中添加。 This works for textbox but not sure with RichTexBox
这适用于文本框,但不确定 RichTexBox
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.