Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...