当在表格1上扫描条形码时,我会调用数据库以获取该条形码对应的物品,并打开表格2并预填充数据。
如果我使用“文本更改事件”,则每个数字都将触发一次该事件。
我无法检查条形码的长度,因为每个条形码的长度可能不同。
我应该使用哪个事件来仅在扫描条形码时进行一次调用?
我尝试了TextChanged、KeyPress和KeyDown事件,但它们都会被多次调用。
我可以使用文本框来捕获表单1上的条形码(我将在用户界面上隐藏它)。
如果我使用“文本更改事件”,则每个数字都将触发一次该事件。
我无法检查条形码的长度,因为每个条形码的长度可能不同。
我应该使用哪个事件来仅在扫描条形码时进行一次调用?
我尝试了TextChanged、KeyPress和KeyDown事件,但它们都会被多次调用。
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();
}
我可以使用文本框来捕获表单1上的条形码(我将在用户界面上隐藏它)。
if (e.KeyCode == Keys.Enter) { e.SuppressKeyPress = true; //( process) }
- Jimiusing()
块进行适当资源清理。 - Tanveer Badar