C# - 单击“删除”按钮后,文本框中显示的值将如何显示?

时间:2017-03-19 06:45:04

标签: c# forms

我正在创建一个POS系统。我只知道如何显示总是提交的数据。但是..当我尝试从列表视图中删除某个产品时,文本框上显示的值不会扣除如何实施代码?

这是代码..

public frm_order() {
 InitializeComponent();

 listView1.View = View.Details;
 listView1.FullRowSelect = true;

 listView1.Columns.Add("Product Name", 150);
 listView1.Columns.Add("Quantity", 100);
 listView1.Columns.Add("Price", 100);
}

private void add(String name, String qty, String price) {
 String[] row = {
  name,
  qty,
  price
 };

 ListViewItem item = new ListViewItem(row);

 listView1.Items.Add(item);
}

int qty;
double price;
double subtotal;
double tax;
double total;
double vat = 0.12;

private void btn_choco_Click(object sender, EventArgs e) {
 txt_name.Text = "Choco Lover";
 txt_price.Text = "65.00";
 txt_quantity.Text = "1";
 price = 65.00;
}
private void btn_tutti_Click(object sender, EventArgs e) {
 txt_name.Text = "Tutti Frutti";
 txt_price.Text = "65.00";
 txt_quantity.Text = "1";
 price = 65.00;
}
private void btn_black_Click(object sender, EventArgs e) {
 txt_name.Text = "Black Forest";
 txt_price.Text = "75.00";
 txt_quantity.Text = "1";
 preprice = 75.00;
}
private void btn_vanilla_Click(object sender, EventArgs e) {
 txt_name.Text = "Vanilla Sky";
 txt_price.Text = "65.00";
 txt_quantity.Text = "1";
 price = 65.00;
}
private void btn_ube_Click(object sender, EventArgs e) {
 txt_name.Text = "Ube My Bebe";
 txt_price.Text = "65.00";
 txt_quantity.Text = "1";
 preprice = 65.00;
}
private void btn_smores_Click(object sender, EventArgs e) {
 txt_name.Text = "Smore's Pa More";
 txt_price.Text = "65.00";
 txt_quantity.Text = "1";
 price = 65.00;
}
private void btn_cookie_Click(object sender, EventArgs e) {
 txt_name.Text = "Cookie Monster";
 txt_price.Text = "65.00";
 txt_quantity.Text = "1";
 price = 65.00;
}
private void btn_rainbow_Click(object sender, EventArgs e) {
 txt_name.Text = "Rainbow Dreamland";
 txt_price.Text = "75.00";
 txt_quantity.Text = "1";
 price = 75.00;
}

private void btn_confirm_Click(object sender, EventArgs e) {
 qty = Convert.toInt32(txt_quantity.Text);
 add(txt_name.Text, txt_quantity.Text, txt_price.Text);

 subtotal = price * qty;
 txt_subtotal.Text = subtotal.ToString();
 tax = subtotal * vat;
 txt_tax.Text = tax.ToString();
 total = subtotal + total;
 txt_total.Text = total.ToString();

 txt_name.Text = "";
 txt_quantity.Text = "";
 txt_price.Text = "";
}

private void delete() {
 if (MessageBox.Show("Are you sure?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) {
  listView1.Items.RemoveAt(listView1.SelectedIndices[0]);
  listView1.Refresh();

  txt_name.Text = "";
  txt_quantity.Text = "";
  txt_price.Text = "";
 }
}

private void btn_del_Click(object sender, EventArgs e) {
 delete();
}

private void update() {
 listView1.SelectedItems[0].SubItems[0].Text = txt_name.Text;
 listView1.SelectedItems[0].SubItems[1].Text = txt_quantity.Text;
 listView1.SelectedItems[0].SubItems[2].Text = txt_price.Text;

 txt_name.Text = "";
 txt_quantity.Text = "";
 txt_price.Text = "";
}
private void btn_update_Click(object sender, EventArgs e) {
 update();
}

private void btn_clear_Click(object sender, EventArgs e) {
 listView1.Items.Clear();

 txt_name.Text = "";
 txt_quantity.Text = "";
 txt_price.Text = "";
}

private void listView1_MouseClick(object sender, MouseEventArgs e) {
 txt_name.Text = listView1.SelectedItems[0].SubItems[0].Text;
 txt_quantity.Text = listView1.SelectedItems[0].SubItems[1].Text;
 txt_price.Text = listView1.SelectedItems[0].SubItems[2].Text;
}

private void btn_payment_Click(object sender, EventArgs e) {
 frm_payment pay = new frm_payment();
 pay.Show();
}

private void frm_order_Load(object sender, EventArgs e) {

}

private void txt_quantity_KeyPress(object sender, KeyPressEventArgs e) {
 if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.')) {
  e.Handled = true;
 }
}

另外,我如何实现显示“订购产品”总数的代码?希望你能回答我。

1 个答案:

答案 0 :(得分:0)

从listView删除后,您需要每次调用update()。删除之后调用listView1.Refresh()如下所示(我测试了它为我工作)

 listView1.Items.RemoveAt(listView1.SelectedIndices[0]);
 listView1.Refresh();