【问题标题】:How to capture whole Barcode value on Winform without using TextChanged event?如何在不使用 TextChanged 事件的情况下在 Winform 上捕获整个 Barcode 值?
【发布时间】:2019-08-20 00:04:43
【问题描述】:

当在 form1 上扫描条形码时,我调用数据库以获取此条形码的项目并使用预先填充的数据打开 form2。

如果我使用文本更改事件,那么它会产生与一个条形码中的数字一样多的次数。

我无法检查条形码的长度,因为它可能每次都不同。

当条形码被扫描时,我应该使用哪个事件来只拨打一个电话?

我尝试了 TextChanged、KeyPress、KeyDown 事件,但它们都被多次调用。

    private void txt_Barcode_TextChanged(object sender, EventArgs e)
    {
        con.Open();
        GenerateInvoice gn = new GenerateInvoice();
        string query = "SELECT * FROM dbo.Inventory WHERE Barcode = '" + txt_Barcode.Text + "' ";

        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader dr = cmd.ExecuteReader();


        while (DR1.Read())
        {
            gn.txt_Barcode.Text = dr["Barcode"].ToString();
            gn.txt_ProductName.Text = dr["ProductName"].ToString();
            gn.txt_Price.Text = dr["SellingPrice"].ToString();
            gn.txt_QTY.Text = 1.ToString();
            gn.txt_Total.Text = dr["SellingPrice"].ToString();

        }
        con.Close();
    }

我愿意使用文本框来捕获 form1 上的条形码(我将在 UI 上隐藏它)

【问题讨论】:

  • 条形码扫描仪在代码完成后发送一个 Enter 键。此外,大多数扫描仪还可以配置为同时发送前缀、后缀或同时发送条形码(例如,识别发送条形码的扫描仪)。使用 KeyDown 事件。例如,if (e.KeyCode == Keys.Enter) { e.SuppressKeyPress = true; //( process) }
  • 此外,您的代码的 sql 方面还有一些不足之处。摆脱硬编码查询,使用参数化,使用using() 块检查适当的资源清理。
  • 当您像使用键盘一样使用条形码扫描仪时,理想情况下只想在看到此事件后触发事件:(去抖动和节流)stackoverflow.com/questions/25991367/…

标签: c# winforms barcode


【解决方案1】:

这是 WedgeMode 扫描仪的结果。基本上它充当键盘,扫描的每个字符都会创建一个文本更改事件。

有很多解决方法。

您可以使用您购买扫描仪的公司提供的 api,而不是楔形模式

但是,一个简单的解决方法是在扫描仪上放置前缀和后缀(如 ascii 代码 STXETX )(通常由扫描仪提供此设置),这样您就知道何时你有完整的条形码数据。

当您看到一个有效的条形码时,您会创建一个事件,而不是为每个扫描的字符创建一个事件。

【讨论】:

    【解决方案2】:

    您可能会看到我对这个主题的一些回答。

    Improve Barcode search in a Textbox C#

    distinguish between the scanner and the keyboard

    Barcode scanner with a WPF application

    如果我要再做一次第一次是很久以前,我会选择 RawInput 并确定哪个设备是条形码扫描仪。使用前缀和后缀是可靠的虽然它们因设备而异捕获原始输入会抽象出这个硬件实现。

    代码项目文章及下载:Using Raw Input from C# to handle multiple keyboards

    了解如何从任何来源获取输入,因此我什至不需要用户将光标放在文本框上或使用 Form.KeyPreview 我可以获取按设备过滤的输入。

    【讨论】:

      【解决方案3】:

      您可以尝试让事件等待 1 秒,或者等待足够长的时间以完成扫描

      private async void txt_Barcode_TextChanged(object sender, EventArgs e)
      {
          await Task.Delay(1000);
          con.Open();
          GenerateInvoice gn = new GenerateInvoice();
          string query = "SELECT * FROM dbo.Inventory WHERE Barcode = '" + txt_Barcode.Text + "' ";
      
          SqlCommand cmd = new SqlCommand(query, con);
          SqlDataReader dr = cmd.ExecuteReader();
      
      
          while (DR1.Read())
          {
              gn.txt_Barcode.Text = dr["Barcode"].ToString();
              gn.txt_ProductName.Text = dr["ProductName"].ToString();
              gn.txt_Price.Text = dr["SellingPrice"].ToString();
              gn.txt_QTY.Text = 1.ToString();
              gn.txt_Total.Text = dr["SellingPrice"].ToString();
      
          }
          con.Close();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多