问题描述
在form1上扫描条形码时,我调用数据库以获取该条形码的项目,并使用预先填充的数据打开form2.
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?
我尝试了TextChanged,KeyPress,KeyDown事件,但是它们都被多次调用了.
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();
}
我愿意使用文本框捕获Form1上的条形码(我将其隐藏在UI中)
I am open to use textbox to capture barcode on form1 (I will hide it on UI)
推荐答案
这是扫描仪处于WedgeMode模式的结果.基本上,它充当键盘,并且扫描的每个字符都会创建一个文本更改事件.
This is a result of the scanner in WedgeMode. basically it acts as a keyboard and every character scanned creates a text changed event.
有很多解决方法.
您可以使用购买扫描仪的公司提供的api代替楔模式
You could use an api supplied by the company you bought the scanner off, instead of wedgemode
但是,一个简单的解决方法是在扫描仪上放置前缀和后缀(例如ascii代码STX
和ETX
)(通常由扫描仪提供此设置),这样您就知道何时您拥有完整的条形码数据.
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.
这篇关于如何在不使用TextChanged事件的情况下在Winform上捕获整个条形码值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!