Hi guys,
I have a listview that displays "product Description" in the first column and "Product Price" in another column, after the "add" button is clicked on frmSales form. After that, txtAmountDue is automatically updated with the value of the items added to the listview.

The Remove Button, when clicked, removes the selected item from the listview.

What I want is the code to deduct the value of the item removed from the listview from the Amount in txtAmountDue, when the remove button is clicked.
I have tried to the code below

Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click


        For ICount = lsv1.Items.Count - 1 To 0 Step -1
            If lsv1.Items(ICount).Selected Then
                lsv1.Items(ICount).Remove()

                Dim pSum As Double
                For Each item As ListViewItem In lsv1.Items
                    pSum -= CInt(item.SubItems(1).Text)

                    txtAMDue.Text = pSum.ToString("N2")
                Next
            End If
            Application.DoEvents()

        Next



    End Sub

But instead, is seemingly is deducting the total from the value of the selected item, giving me a negative figure.

May someone help me please.

Dani AI

Generated

You ran into a classic sign flip, : you removed the item, then started from 0 and subtracted every remaining price, so the running total goes negative. Two safe patterns are: (1) recompute the total from what remains after removal, or (2) keep a running Decimal and subtract only the price(s) of the items being removed. I agree with and on the general idea, but for money use Decimal (not Double), and parse with currency-aware TryParse so labels like "1,234.50" or "$9.99" do not blow up. Also, no need for Application.DoEvents here.

VB.NET (WinForms) example: remove first, then recalc the total from the ListView. This handles multi-select and any formatting in the price column.

Private Function SumPrices(lv As ListView, priceIndex As Integer) As Decimal
    Dim total As Decimal = 0D
    For Each it As ListViewItem In lv.Items
        Dim price As Decimal
        If Decimal.TryParse(it.SubItems(priceIndex).Text,
                            Globalization.NumberStyles.Currency,
                            Globalization.CultureInfo.CurrentCulture, price) Then
            total += price
        End If
    Next
    Return total
End Function

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    If lsv1.SelectedItems.Count = 0 Then Exit Sub
    Dim toRemove As New List(Of ListViewItem)(lsv1.SelectedItems.Cast(Of ListViewItem))
    For Each it In toRemove
        it.Remove()
    Next
    txtAMDue.Text = SumPrices(lsv1, 1).ToString("N2")
End Sub

And for (C#), using CheckedItems and the same recompute approach:

using System.Globalization;
decimal SumPrices(ListView lv, int priceIndex)
{
    decimal total = 0m;
    foreach (ListViewItem it in lv.Items)
        if (decimal.TryParse(it.SubItems[priceIndex].Text,
            NumberStyles.Currency, CultureInfo.CurrentCulture, out var price))
            total += price;
    return total;
}

private void button4_Click(object sender, EventArgs e)
{
    var remove = listView1.CheckedItems.Cast<ListViewItem>().ToList();
    foreach (var it in remove) listView1.Items.Remove(it);
    label5.Text = SumPrices(listView1, 1).ToString("N2");
}

Tip: if you later bind real product objects, store the Decimal price in ListViewItem.Tag and skip parsing entirely.

Recommended Answers

All 7 Replies

Try this. You will have to change the naming. I have a Label which is Label2 which acts as a Total price or (Current Total) and I'm using ListView4.

 For Each iItem As ListViewItem In ListView4.SelectedItems
 ' I declared the variables I will use to keep track here.
 Dim Price_To_Deduct As Integer ' I used Integer because I didn't format the numbers to money format so you can try using Double but I haven't tested that.
 Dim Current_Total_Price As Integer ' This variable will contain the current total price which we will deduct from.
 Dim New_Total As Integer ' This will contain the new total.

 Price_To_Deduct = iItem.SubItems(1).Text
 Current_Total_Price = Label2.Text ' Retrieving the current total price so that we will do calculations.
 New_Total = Current_Total_Price - Price_To_Deduct
 ' Because I'm using a Label I now have to clear it and reUpdate it with the new total.
 Label2.Text = ""
 Label2.Text = New_Total
 Next

This should work.

More shortly the codes should be

For Each iItem As ListViewItem In ListView4.SelectedItems

    Label2.Text = Val(Label2.Text) - Val(iItem.SubItems(1).Text)

    'And also remove the selected Item here
Next

Hope it can help you.

It worked guys, thank you.

That's great. I think you may now mark this question as solved then.

how can you do this in microsoft visual c#?

Don't use to hijack other peoples posts. Here is a C# code.

foreach (ListViewItem iItem in ListView4.SelectedItems) {
    // I declared the variables I will use to keep track here.
    int Price_To_Deduct = 0;
    // I used Integer because I didn't format the numbers to money format so you can try using Double but I haven't tested that.
    int Current_Total_Price = 0;
    // This variable will contain the current total price which we will deduct from.
    int New_Total = 0;
    // This will contain the new total.
    Price_To_Deduct = iItem.SubItems(1).Text;
    Current_Total_Price = Label2.Text;
    // Retrieving the current total price so that we will do calculations.
    New_Total = Current_Total_Price - Price_To_Deduct;
    // Because I'm using a Label I now have to clear it and reUpdate it with the new total.
    Label2.Text = "";
    Label2.Text = New_Total;
}

im sorry i got this button for removing in the list

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";

                     }
                 }
                 else
                 {
                 }
             }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.