【发布时间】:2017-05-17 15:58:08
【问题描述】:
我有一个 ListView 和一个按钮。当我点击它时,它会在我的标签中添加一个(名称、价格、数量、总计)和总金额,问题是当我在 ListView 中删除选中的项目时如何减少标签中的总金额
顺便说一句,我的总计和价格数据类型不是在按钮内部的形式,按钮内部是价格、总计和计算的金额。
这是 ListView 的删除按钮
private void button4_Click(object sender, EventArgs e)
{
if (listView1.CheckedItems.Count > 0)
{
var confirmation = MessageBox.Show("Are you sure?", "Confirmation",
MessageBoxButtons.YesNo);
if (confirmation == DialogResult.Yes)
{
for (int i = listView1.CheckedItems.Count - 1; i >= 0; i--)
{
ListViewItem itm = listView1.CheckedItems[i];
listView1.Items[itm.Index].Remove();
label5.Text = "0"; //label for total amount
}
}
else
{
}
}
}
这是在ListView中添加项目的代码
private void addItem_Click(object sender, EventArgs e)
{
ListViewItem item = listView1.FindItemWithText("Cola");
if (String.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("Enter Quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox2.Text = "";
}
else if (textBox2.Text == "0")
{
MessageBox.Show("Enter Valid Quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (item != null)
{
price = 500;
quantity = Double.Parse(textBox2.Text);
total = price * quantity;
item.SubItems[2].Text = quantity.ToString();
item.SubItems[3].Text = total.ToString();
textBox2.Text = "";
totalAll = total + total2;
label5.Text = totalAll.ToString();
}
else if (item == null)
{
price = 500;
quantity = Double.Parse(textBox2.Text);
total = price * quantity;
String[] row = { label1.Text, price.ToString(), quantity.ToString(), total.ToString() };
item = new ListViewItem(row);
listView1.Items.Add(item);
textBox2.Text = "";
totalAll = total + total2;
label5.Text = totalAll.ToString();
}
}
【问题讨论】:
-
你能显示添加列表视图项的代码吗?这样我们就可以看到您将项目的
total属性存储在哪里? -
您的第一个答案出现错误,我提供了添加项目的代码,请检查,谢谢!
-
好的,我更新了答案。这假定
totalAll是一个可以从按钮代码访问的类级别变量。如果不是,应该是。 -
是的
totalAll是类级变量
标签: c# visual-studio listview