Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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();
        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.
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();
        }
    }
}


What I have tried:

I've tried eventually by changing in public method:

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


But it didn't help
Posted
Updated 25-Oct-21 22:58pm

1 solution

You would need to contact the author of your library and check with them: it isn't a standard part of .NET.

If they are unhelpful, then you will have to process scanner input yourself. Most scanners come set to act as a keyboard, and there is no way to tell if a particular keystroke comes from a "real" keyboard or a scanner as Windows does not record the source at all.

If you want scanned data to always go to a specific place, you need to tell the scanner to provide "lead in" and "tail out" sequences that you code can spot and use to identify scanned data.

Contact the scanner manufacturer and they will be able to tell you how to enable such on your particular instrument.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900