0

I'm using a listview as a shopping cart. I need to know how to recalculate the total value of the cart when I remove an item.

Here is my code for adding to listview;

private void btnACart_Click(object sender, EventArgs e)
{
    int value = 0;

    for (int i = 0; i < lvCart.Items.Count; i++)
    {
        value += int.Parse(lvCart.Items[i].SubItems[1].Text);
    }

    rtbTcost.Text = value.ToString();
}

Here is my code for removing items:

private void btnRemoveItem_Click(object sender, EventArgs e)
{
    int total = 0;
    foreach (ListViewItem item in lvCart.Items)
    {
        if (lvCart.Items[0].Selected)
        {
            lvCart.Items.Remove(lvCart.SelectedItems[0]);
            total += Convert.ToInt32(item.SubItems[1].Text);
        }
    }

    rtbTcost.Text = total.ToString();
}

I want to recalculate the total value of items an item is removed. How should I do that?

2
  • Could you, please, clarify what total you're asking. For me it did not open up, while reading the question. Commented Feb 15, 2014 at 19:28
  • I want the listview to act as a cart. I'm trying to get the total price of every item. Commented Feb 15, 2014 at 19:33

3 Answers 3

1

Something like this

On the form level declare

private int _listTotal;

Adding - I think here you have some problems because you should add to total when you add the item

private void btnACart_Click(object sender, EventArgs e)
{
    int value = 0;

    for (int i = 0; i < lvCart.Items.Count; i++)
    {
        value += int.Parse(lvCart.Items[i].SubItems[1].Text);
    }
    // how about lvCart.Items.Add(<myVal>)...???
    _listTotal += value; // and here add myVal
    rtbTcost.Text = _listTotal.ToString();
}

Then when removing - you don't want to use any "for-loops" on mutating collection. But "while" works perfectly on mutations

private void btnRemoveItem_Click(object sender, EventArgs e)
{
    int totalRemoved = 0;
    while (lvCart.SelectedItems.Count > 0)
    {
        totalRemoved += Convert.ToInt32(lvCart.SelectedItems[0].SubItems[1].Text);
        lvCart.Items.Remove(lvCart.SelectedItems[0]);
    } 
    _listTotal -= totalRemoved;
    rtbTcost.Text = _listTotal.ToString

}

Not tested but should work

Sign up to request clarification or add additional context in comments.

11 Comments

thanks for the help sir. Not really that accurate but it worked fine.
Excuse me sir regarding to this program. My problem in the code you gave to me is that the every time I remove the item the value that it is removing is the 1st value added into the listview @T.S.
@bluffer I just tested it - it works fine. I have list view with _l.MultiSelect = true;. I've added 10 items, selected 3 of them, for example items, 2,5,8. And they got removed perfectly by ` while (_l.SelectedItems.Count > 0) { _l.Items.Remove(_l.SelectedItems[0]);}` I see no problem. You need to actually click and select the item before removing it.
thanks again sir hahaha. I tried using for loop and combined your code. But it now works fine thank you very much!!!!
I warned you - don't use for-loop for mutating collections!! :o))
|
0

You can't change the same list in foreach.

foreach (ListViewItem item in lvCart.Items)
{
    if (lvCart.Items[0].Selected)
    {
        lvCart.Items.Remove(lvCart.SelectedItems[0]);
        total += Convert.ToInt32(item.SubItems[1].Text);
    }
}

The solution is create duplicate list and change it:

var newList = lvCart;

foreach (ListViewItem item in lvCart.Items)
{
    if (lvCart.Items[0].Selected)
    {
        newList.Items.Remove(lvCart.SelectedItems[0]);
        total += Convert.ToInt32(item.SubItems[1].Text);
    }
}

Comments

0
private void btnRemoveItem_Click(object sender, EventArgs e)
{
    int total = 0;
    foreach (ListViewItem item in lvCart.Items)
    {
       if (lvCart.Items[0].Selected)
        {
            total+=(int)lvCart.SelectedItems[0].SubItems[1].Text;//Add to total while romving
           lvCart.Items.Remove(lvCart.SelectedItems[0]);
           //total += Convert.ToInt32(item.SubItems[1].Text);
        }
    }

      rtbTcost.Text = total.ToString();
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.