3

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.

Prochu1991
  • 443
  • 5
  • 20
  • 3
    You shouldn't need a library at all, the scanner will act as a keyboard – Charlieface Oct 26 '21 at 08:27
  • Can you show the code for your USB_Barcode_Scanner namespace? One way to avoid keyboard / focus issues is to use a virtual COM driver. Another way would be to configure the scanner to send specific key sequences before the barcode data. But without seeing the USB_Barcode_Scanner it's hard to say what it's trying to do. – user700390 Nov 03 '21 at 03:39
  • @user700390 I downloaded just this dll file from this link: https://jumpshare.com/v/OaBfkKCNr8duXzMXoakB and I added to Refernce to my project. – Prochu1991 Nov 03 '21 at 07:54
  • If you don't have the source code or documentation, it's probably easier to avoid using that library. Is the scanner configured to use keyboard or serial / virtual com port interface? – user700390 Nov 04 '21 at 18:17
  • @user700390 It's configured that only scans any barcode then enter key and scans again. – Prochu1991 Nov 05 '21 at 06:54
  • @Prochu1991 is the issues is resolved? if not, first please remove the barcode object and event handler from the code. call the `textBox1.Focus();` after `Fillcombox();` . i have developed multiple MES systems but never used any library to input the barcode data. scanning will work just like normal keyboard input. – Yogesh Naik Nov 09 '21 at 05:01

2 Answers2

4

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.

user700390
  • 2,287
  • 1
  • 19
  • 26
1

The scanner can be [re]configure to send prefix and suffix before the actual barcode.

You can then use those special character for your internal logic about field change.

By default scanner send the number followed by "enter", if the focus is on a specific textbox, that one will received the number.

If you don't want to reconfigure the scanner, you can then add logic when "enter" is type in the text field to MOVE the focus to the next control.

Zyo
  • 1,934
  • 21
  • 25