[英]how can I show the data automatically after I input the data into a textbox? C#
我想當我輸入條碼號而不按任何按鈕時,數據會自動顯示,默認情況下數量為 1 我嘗試與txt_no
相同,但它也需要按回車按鈕。
這是表格。 在此處輸入圖片說明
這是代碼:
private void txtno_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtqty.Enabled = true;
txtqty.Focus();
}
}
private void txtqty_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
try
{
string txt="Select * from products where id = '"+txtno.Text+"'";
MySqlCommand cmd = new MySqlCommand(txt,con);
con.Open();
MySqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
int price = int.Parse(txtqty.Text.Trim()) * int.Parse(r[4].ToString());
totalprice = totalprice + price;
dataGridView1.Rows.Add(dataGridView1.RowCount, r[0], r[1], txtqty.Text.Trim(), r[4], price);
}
lbitems.Text = "" + (dataGridView1.RowCount - 1) + "";
lbtotal.Text = "" + totalprice + "";
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error From Database");
}
txtno.Focus();
txtno.Clear();
txtqty.Enabled = false;
txtqty.Clear();
}
}
您需要 txtno(即條形碼文本框)的TextChanged
事件處理程序。
為 txtno 添加TextChanged
事件處理程序,如下所示。
正如@HansKesting 所建議的,您應該清理輸入以避免 sql 注入。
private void txtno_TextChanged(object sender, KeyPressEventArgs e)
{
if(!string.IsNullOrEmpty(txtno.Text))
{
txtqty.Enabled = true;
txtqty.Focus();
Search();
}
else
{
txtqty.Enabled = false;
}
}
private void txtqty_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
Search();
}
}
private void Search()
{
try
{
string txt = "Select * from products where id = '" + txtno.Text + "'";
MySqlCommand cmd = new MySqlCommand(txt, con);
con.Open();
MySqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
int price = int.Parse(txtqty.Text.Trim()) * int.Parse(r[4].ToString());
totalprice = totalprice + price;
dataGridView1.Rows.Add(dataGridView1.RowCount, r[0], r[1], txtqty.Text.Trim(), r[4], price);
}
lbitems.Text = "" + (dataGridView1.RowCount - 1) + "";
lbtotal.Text = "" + totalprice + "";
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error From Database");
}
txtno.Focus();
txtno.Clear();
txtqty.Enabled = false;
txtqty.Clear();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.