【问题标题】:Reduce total amount when remove in ListView在 ListView 中删除时减少总量
【发布时间】: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


【解决方案1】:

如果您要保留 ListBox 中所有项目的总和,则只需减去要删除的项目的总和:

// Subtract selected item total from grand total
// This assumes that your 'totalAll` variable is a class-level double variable 
// and that the item total is stored in the `Text` property of itm.SubItems[3]
totalAll = totalAll - double.Parse(itm.SubItems[3].Text); 

label5.Text = totalAll.ToString();

【讨论】:

  • 感谢它的工作!但label5.Text 的值不会重置,如果我删除该项目并再次重新添加该值将保持
  • 当我删除并再次添加时,该值仍然存在,但它在第一次删除时起作用
  • 我不知道您是说重新添加项目不会再次将总数加回来,还是说第二次删除项目不会减去项目总数。您可以在“添加”或“删除”块内的第一行代码上设置断点(无论哪个给您带来麻烦),然后单步执行代码以查看流程。例如,在您的 add 代码中,它不会显示设置 total2 的位置,但该代码似乎不会将当前项目总计添加到总计中。
  • 这是我在类级别变量 public static Double price, quantity, total, total2, totalAll; 中的数据类型,所以在 addItem_Click "button" = total2 和 addItem1_Click "button" = total 中
  • 它是在第一次删除项目时减去总金额,但如果我再次重新添加项目,该值仍然存在,就像label5.Text 正在保存一个值?或者有没有办法重置这个值?
【解决方案2】:

在循环之后,我将它设置为当前的项目数:

if ( ...confirm... )
{
    for ( ... )
    {
    }

    label5.Text = listView1.Items.Count.ToString();
}

【讨论】:

  • 谢谢,但它并没有减少总量,它只是计算剩余的项目,我怎样才能减少总量?列表视图中的示例:第 1 行 = (price = 300) 第 2 行 = (price = 500) 如果我删除第 2 行,它将仅在 label5.Text 中显示第 1 行的价格
猜你喜欢
  • 1970-01-01
  • 2012-10-31
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多