Search code examples
c#textboxbarcodescanning

How to scan barcode into textbox by zebra DS3608 - C#


I'm writing app which can not only generate barcode from combobox but will scan barcode into textbox. Generating barcode is working well but is a little worse with scanning it by Device Zebra DS3608. In this project I added a library:

using USB_Barcode_Scanner;

And then I added and modified in public method:

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

And new method was automatically generated.

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

After this modification it compiled correctly but after launching it instead focusing on textbox I try to scan but it begins from button and scans into combobox in below is image of the app: Concept of PrintLabel App

How can i change it? Does it belong from this barcode scanner settings or this code? Maybe below code helps:

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();
        }
    }
}

Thank you in advance.


Solution

  • Based on the question and comments, here is a basic outline of a viable solution:

    • Configure the scanner to send ENTER both before AND after the barcode data is transmitted
    • Remove the USB_Barcode_Scanner namespace and all references to the events it exposes
    • Set up a default button on the form so whenever ENTER is pressed the click event of the button will be executed
    • In the event handler for default button, check if the barcode input text box is empty
    • If the text box is empty, set focus to the text box
    • If the text box has text, process the text in the text box and replace it with empty string

    If you make these adjustments, when the first ENTER is input by the barcode scanner, the focus will change to the input text box, which will receive the barcode data, and the second ENTER will allow you to process the data.

    Also, if you can determine how to conifgure the scanner to send ESC before the barcode data, you can set a cancel button on the form and set focus to the input box on the cancel button, this might make the process more robust, but you will still need to ensure the application has keyboard focus.


    Another option here would be to reconfigure the scanner for a Virtual COM port, and use a SerialPort to read the barcode input. In the SerialPort data received event, you can capture/buffer the data while monitoring for the stop sentinel, and then process when the stop sentinel is received. This has the added convenience that it will work even if the application doesn't have keyboard focus.