简体   繁体   English

让用户在 winform 弹出时开始输入,而无需单击文本框

[英]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.

相关问题 如何设置Winform文本框字段焦点,以便用户可以通过单击“选项卡”按钮来浏览它们? - How to Set Up Winform Textbox Field Focus so a User Can Go Through Them by Clicking Tab Button? 导致数字在文本框中输入时一个接一个地出现,但也会在出现0时将它们相加 - cause numbers to appear one after another when typing in textbox but also make them add up when 0 appears 当键盘弹出时,页面没有向上滚动到最后一个元素(文本框) - Page is not scrolling upto last element(TextBox) when keyboard pops up 用户开始输入时自动激活文本框 - Activate a textbox automatically when user starts typing 当用户开始输入时使TextBox清除 - Making TextBox Clear when user starts typing 当用户在Winform TextBox中键入内容时,如何动态检查文件或目录是否存在 - How to dynamically check if the file or directory exists while user is typing in Winform TextBox 捕获用户单击并键入自定义的类似WPF TextBox的控件? - Capture user click and typing in a custom WPF TextBox-like control? 你好。 我找到了年龄。 在 textbox1 中输入用户 DOB 后,年龄应自动显示在 textbox2 中,没有任何点击事件 - hello. i find age. after typing user DOB in textbox1 the age should be shown in textbox2 automatic without any click event 在C#winform中加载用户控件时,将焦点放在文本框中 - put focus in textbox when user control is load in C# winform C#:当用户开始键入时激活一个TextBox - C#: Activate a TextBox when user starts typing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM