Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If you have a list of items in a ListView which have their total displayed in a Label (lblDisplay.Text) and you delete one of the values, how can you subtract the deleted value from the total automatically when you click the Remove Item button (btnRemove)?
The Code:
VB
Private Sub Apple_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Apple.Click
Dim MyItem As ListViewItem
Dim A As Single = 5
        If txtDisplay.Text = "" Then 'txtDisplay is a textbox where I put number of items the customer is purchasing.
            MyItem = ListView1.Items.Add("Apple 1 @ $5", 0)
            MyItem.SubItems.Add(A)
            lblTotal.Text = Val(lblTotal.Text) + Val(A)
        Else
            AnswerNumber = A * txtDisplay.Text
           MyItem = ListView1.Items.Add("Apple " & txtDisplay.Text & " @ $5", 0)
           MyItem.SubItems.Add(AnswerNumber)
            lblTotal.Text = Val(lblTotal.Text) + Val(AnswerNumber)
        End If
End Sub


This is the Code to Remove Item:
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
ListView1.Items.Remove(ListView1.SelectedItems.Item(index:=0))
End Sub
Posted
Updated 12-Nov-10 7:38am
v3
Comments
Naa Nana 12-Nov-10 14:44pm    
Hi, thanks for posting an answer. My problem is that the AnswerNumber is only tied to each product so when you click on the product button the code adds the value. For Example:
Purchasing Item Price
Apple $ 5 (btnApple) button gives me this
Orange $ 2 (btnOrange) button gives me this

Total $ 7 (AnswerNumber) gives me this in lblTotal

I don't know how to subtract for instance Apple's value from the total when I click Remove Item button.
Naa Nana 13-Nov-10 9:01am    
After a long search, I found a solution to the problem. This is the Code:
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
ListView1.Items.Remove(ListView1.SelectedItems.Item(index:=0))
Dim sum As Long = 0
For Each lvItem As ListViewItem In ListView1.Items
sum += CInt(lvItem.SubItems(1).Text).ToString
lblTotal.Text = ""
Next
lblTotal.Text = sum
End Sub

1 solution

On the Sub btnRemove_Click , you can get the the value of AnswerNumber.
You can do the calculation here itself by subtracting the Value of AnswerNumberm the value of lblTotal.Text .

-------------------------------------
Hope this will give you at least a spark.
A spark is enough for a big bang :)
 
Share this answer
 
Comments
Naa Nana 13-Nov-10 8:24am    
Hi, thanks for posting an answer. My problem is that the AnswerNumber is only tied to each product so when you click on the product button the code adds the value.
For Example: Purchasing Item Price
Apple $ 5 (btnApple) button gives me this
Orange $ 2 (btnOrange) button gives me this
Total $ 7 (AnswerNumber) gives me this in lblTotal
I don't know how to subtract for instance Apple's value from the total when I click Remove Item button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900