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.

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.