Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture whole Barcode value on Winform without using TextChanged event?

When a barcode is scanned on form1, I make a call to database to get the item for this barcode and open form2 with pre-populated data.

If I use text changed event then it makes as many times as numbers in one barcode.

I cannot check length of the barcode as it may be different each time.

which event I should use to make only one call when Barcode is scanned?

I tried TextChanged, KeyPress, KeyDown events but they all are being called multiple times.

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

I am open to use textbox to capture barcode on form1 (I will hide it on UI)

like image 307
newdeveloper Avatar asked Mar 29 '19 05:03

newdeveloper


2 Answers

This is a result of the scanner in WedgeMode. basically it acts as a keyboard and every character scanned creates a text changed event.

There are many solves.

You could use an api supplied by the company you bought the scanner off, instead of wedgemode

However, a simple solve is to put a prefix and suffix (like the ascii codes, STX and ETX ) on the scanner (there are usually settings for this supplied by the scanner), that way you know when you have complete bar-code data.

When you see a valid barcode, then you make one event, not an event for each character scanned.

like image 58
TheGeneral Avatar answered Sep 28 '22 16:09

TheGeneral


You'll probably see a few of my answers on this topic.

Improve Barcode search in a Textbox C#

distinguish between the scanner and the keyboard

Barcode scanner with a WPF application

If I was to do it again and the first time was a long time ago, I'd opt for RawInput's and determine which device is the BarCode scanner. Using prefix and suffix are reliable although they vary depending on the device. Capturing the raw input abstracts this hardware implementation.

Code Project article and download: Using Raw Input from C# to handle multiple keyboards

See how I can get input from any source so I dont even need the user to put a cursor on a textbox or use Form.KeyPreview I can get the input filtered by device.

enter image description here

enter image description here

enter image description here

like image 29
Jeremy Thompson Avatar answered Sep 29 '22 16:09

Jeremy Thompson