c# - 如何通过 zebra DS3608 - C# 将条码扫描到文本框中

标签 c# textbox barcode scanning

我正在编写的应用程序不仅可以从组合框生成条码,还可以将条码扫描到文本框中。生成条码运行良好,但通过 Device Zebra DS3608 扫描条码时效果稍差。在这个项目中我添加了一个库:

using USB_Barcode_Scanner;

然后我在公共(public)方法中添加和修改:

public PrintLabel()
        {
        InitializeComponent();
        textBox1.Focus();
        BarcodeScanner barcodeScanner = new BarcodeScanner(textBox1);
        barcodeScanner.BarcodeScanned += BarcodeScanner_BarcodeScanned;
        Fillcombox();
        }

并自动生成新方法。

  private void BarcodeScanner_BarcodeScanned(object sender, BarcodeScannerEventArgs e)
        {
            textBox1.Text = e.Barcode;
        }

此修改后它编译正确但在启动它而不是专注于文本框后我尝试扫描但它从按钮开始扫描到组合框下面是应用程序的图像: Concept of PrintLabel App

我该如何改变它?它属于此条形码扫描器设置还是此代码?也许下面的代码有帮助:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Zen.Barcode;
using USB_Barcode_Scanner;

namespace IT_equipment_program
{
    public partial class PrintLabel : Form
    {
        MySqlConnection connection;
        MySqlCommand command;
        MySqlDataReader dr;
        public PrintLabel()
        {
            InitializeComponent();
            textBox1.Focus();
            BarcodeScanner barcodeScanner = new BarcodeScanner(textBox1);
            barcodeScanner.BarcodeScanned += BarcodeScanner_BarcodeScanned;
            Fillcombox();
        }

        private void BarcodeScanner_BarcodeScanned(object sender, BarcodeScannerEventArgs e)
        {
            textBox1.Text = e.Barcode;
        }

        void Fillcombox()
        {
            try
            {
                string con = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
                MySqlConnection SelectConnection = new MySqlConnection(con);

                string Selectquery = "SELECT DISTINCT Equipment FROM equipments";
                MySqlCommand com = new MySqlCommand(Selectquery, SelectConnection);
                SelectConnection.Open();

                MySqlDataReader dr = com.ExecuteReader();

                while (dr.Read())
                {
                    cmb_Equip.Items.Add(dr.GetString("Equipment"));
                }

                SelectConnection.Close();
            }

            catch
            {
                MessageBox.Show("please check internet connection.");
            }
        }

        void Fillcombox2()
        {
          try
          {
            string conn2 = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
            string query2 = "SELECT IT_Equipments_No FROM equipments WHERE Equipment = @EQUIP";

            using (connection = new MySqlConnection(conn2))
            {
                using (command = new MySqlCommand(query2, connection))
                {
                    command.Parameters.AddWithValue("@EQUIP", cmb_Equip.Text);
                    connection.Open();

                    command.ExecuteNonQuery();
                    dr = command.ExecuteReader();
                    while (dr.Read())
                    {
                        cmb_IT_Equip_NO.Items.Add(dr.GetString("IT_Equipments_No"));
                    }
                    connection.Close();
                }
            }
          }

          catch
          {
              MessageBox.Show("Please check internet connection.");
          }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cmb_Equip_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string conn3 = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
                string query3 = "SELECT Equipment FROM equipments WHERE Equipment = @EQUIP";

                using (connection = new MySqlConnection(conn3))
                {
                    using (command = new MySqlCommand(query3, connection))
                    {
                        command.Parameters.AddWithValue("@EQUIP", cmb_Equip.Text);
                        connection.Open();

                        command.ExecuteNonQuery();
                        dr = command.ExecuteReader();
                        while (dr.Read())
                        {
                            string name = (string)dr["Equipment"].ToString();
                            txt_equip.Text = name;
                            cmb_IT_Equip_NO.SelectedIndex = -1;
                        }
                        cmb_IT_Equip_NO.Enabled = true;
                        cmb_IT_Equip_NO.Items.Clear();
                        Fillcombox2();
                        connection.Close();
                    }
                }
            }

            catch
            {
                MessageBox.Show("Please check internet connection.");
            }
        }

        private void cmb_IT_Equip_NO_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string conn4 = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
                string query4 = "SELECT IT_Equipments_No FROM equipments WHERE IT_Equipments_No = @EQUIP_NO";

                using (connection = new MySqlConnection(conn4))
                {
                    using (command = new MySqlCommand(query4, connection))
                    {
                        command.Parameters.AddWithValue("@EQUIP_NO", cmb_IT_Equip_NO.Text);
                        connection.Open();

                        command.ExecuteNonQuery();
                        dr = command.ExecuteReader();
                        while (dr.Read())
                        {
                            string number = (string)dr["IT_Equipments_No"].ToString();
                            txt_IT_NO.Text = number;
                        }
                        btn_generate.Enabled = true;
                        connection.Close();
                    }
                }
            }

            catch
            {
                MessageBox.Show("Please check internet connection.");
            }
        }

        private void btn_init_Click(object sender, EventArgs e)
        {
            cmb_IT_Equip_NO.SelectedIndex = -1;
            cmb_Equip.SelectedIndex = -1;
            cmb_IT_Equip_NO.Items.Clear();
            cmb_Equip.Items.Clear();

            btn_Print.Enabled = false;
            btn_generate.Enabled = false;
            cmb_IT_Equip_NO.Enabled = false;

            txt_equip.Text = string.Empty;
            txt_IT_NO.Text = string.Empty;
            Fillcombox();
        }

        private void btn_generate_Click(object sender, EventArgs e)
        {
            Code128BarcodeDraw Barcode = BarcodeDrawFactory.Code128WithChecksum;
            pictureBox1.Image = Barcode.Draw(txt_IT_NO.Text, 50);
            btn_Print.Enabled = true;
        }

        private void btn_Print_Click(object sender, EventArgs e)
        {
            PrintDialog pd = new PrintDialog();
            PrintDocument pDoc = new PrintDocument();
            pDoc.PrintPage += PrintPicture;
            pd.Document = pDoc;
            if (pd.ShowDialog() == DialogResult.OK)
            {
                pDoc.Print();
            }
        }

        private void PrintPicture(object sender, PrintPageEventArgs e)
        {
            Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            e.Graphics.DrawImage(bmp, 0, 0);
            bmp.Dispose();
        }
    }
}

提前谢谢你。

最佳答案

根据问题和评论,这里是一个可行解决方案的基本轮廓:

  • 将扫描仪配置为在条码数据传输之前和之后发送 ENTER
  • 删除 USB_Barcode_Scanner 命名空间以及对它公开的事件的所有引用
  • 在窗体上设置一个默认按钮,以便每当按下 ENTER 时将执行按钮的单击事件
  • 在默认按钮的事件处理程序中,检查条码输入文本框是否为空
  • 如果文本框为空,则将焦点置于文本框
  • 如果文本框有文本,处理文本框中的文本,替换为空字符串

如果您进行这些调整,当条形码扫描器输入第一个 ENTER 时,焦点将更改为输入文本框,该文本框将接收条形码数据,第二个 ENTER 将允许您处理数据。

此外,如果您可以确定如何配置扫描器在条码数据之前发送 ESC,您可以在表单上设置一个取消按钮并将焦点设置到取消按钮上的输入框,这可能会使流程更加健壮, 但您仍然需要确保应用程序具有键盘焦点。


此处的另一个选项是为虚拟 COM 端口重新配置扫描仪,并使用串行端口读取条码输入。在 SerialPort 数据接收事件中,您可以在监视停止标记的同时捕获/缓存数据,然后在接收到停止标记时进行处理。这具有额外的便利性,即使应用程序没有键盘焦点,它也能正常工作。

关于c# - 如何通过 zebra DS3608 - C# 将条码扫描到文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69718718/

相关文章:

c# - Keyboard.Focus 不适用于 WPF 中的文本框

java - iReport 中的 google.zxing 条形码生成器

.NET 2.0 中的 C# List<T>.ConvertAll

c# - OpenPop.NET - 修改电子邮件中的字符

c# - 如何从包含泛型参数的Type中获取 'basic'类型?

wpf - 如何使用 Enum 属性中的 DataTrigger?

c# - RichTextBox(如何编辑类似记事本的格式?)

c# - 从 TextBox 获取 .Text 值

c# - 使用网络摄像头读取条形码

c# - 将 tiff 像素长宽比更改为正方形