当条形码扫描器发送以换行符结尾的数据时,如何离焦按钮 [英] How to defocus a button when barcode scanner sent data ending with newline

查看:237
本文介绍了当条形码扫描器发送以换行符结尾的数据时,如何离焦按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个C#条形码应用程序。我有一个EAN-13正则表达式来检测Form1_KeyPress函数中的条形码。我没有机制来检测输入来自哪里。这是我的问题:

我有一个重置按钮的形式,清除所有在dataGridView中列出的字段和条形码。当我点击它,它正常的焦点。当它有焦点时,如果我通过条形码扫描器读取条形码,则每个条形码读取结束时的换行使该按钮被点击,从而清除所有的字段。所以条形码读取被添加到dataGridView,但立即删除,由于重置按钮的激活。

我目前的解决方案是专注于一个只读的文本框在每个 button_Click函数,但我不想在按钮的每个单击功能的末尾写一个不相关的行。你有什么建议? (顺便说一下,我不能防止在表单的keydown函数中输入enter键)

解决方案

表单的击键事件,因为它是由按钮处理的。



如果您添加:

  {
if(e.KeyCode == Keys.Enter)
{
e.IsInputKey = true;




$ p

到一个按钮然后回车键不会导致按钮被点击,你会看到一个Form_KeyDown事件。

你不想把它添加到每个按钮,所以创建一个简单的UserControl是只是一个添加了此代码的按钮。



更新

为空间吧工作。如果你设置 form.KeyPreview = true 并且添加:

  private void form_KeyDown(object sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.Space)
{
e.Handled = true;


然后空格键不会按下按钮它仍然会在文本框中工作。



我不知道为什么Space和Enter有不同的表现。


I am writing a C# barcode application. I have a EAN-13 regex to detect barcodes in "Form1_KeyPress" function. I have no mechanism to detect where the input comes from. Here is my problem:

I have a reset button in the form which clears all fields and barcodes listed in a dataGridView. When I clicked on it, it gets focus as normal. When it has focus, if I read a barcode via barcode scanner, the newline at the end of each barcode reading causes this button to be clicked thus clearing all fields. So barcodes read are added to dataGridView but immediately deleted due to activation of reset button.

My current solution is to focus on a read-only textbox at the end of each "button_Click" function, but I don't want to write an irrelevant line at the end of each "click" function of buttons. What do you recommend? (by the way I cannot prevent surpress enter key in form's keydown function)

解决方案

You can't capture the Enter key in the form's keystroke events because it is handled by the button.

If you add:

private void button_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        e.IsInputKey = true;
    }
}

to a button then the Enter key won't cause the button to be clicked, and you will see a Form_KeyDown event for it.

You don't want to add this to every button, so create a simple UserControl which is just a button with this code added.

Update

This doesn't work for the space bar. If you set form.KeyPreview = true and add:

private void form_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Space)
    {
        e.Handled = true;
    }
}

then the space bar won't press the button but it will still work in text boxes.

I don't know why Space and Enter behave differently.

这篇关于当条形码扫描器发送以换行符结尾的数据时,如何离焦按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆