application just does not add

Edoloto

Member
Joined
Sep 20, 2006
Messages
6
Programming Experience
Beginner
ufffffffffff !!!! man .... you guys are going to laugh .... but I have to ask this question because I've been trying to figure it out for more than 6 hours already.

I'm doing a application (for my class) that requires me to add a item that is keyed from a text box to a list box. I got that.

The problem is that when I'm trying to add(sum) the values in the list box to a label call lblSubTotal, the application just does not add.

My current code for the btnAdd is:

VB.NET:
PrivateSub btnAdd_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles btnAdd.Click
 
Dim ItemPrice AsDouble
Dim SubTotal AsDouble
Dim Sale AsDouble
Dim Count AsInteger
 
 
ItemPrice = CDbl(txtItemPrice.Text)
 
Sale = txtItemPrice.Text
 
SubTotal = SubTotal + ItemPrice
lblSubTotal.Text = SubTotal
lstSales.Items.Add(txtItemPrice.Text)
txtItemPrice.Text = ""
 
EndSub
If someone can help me try to add(sum) the values to a label ... man .. I would be really in debt.

Thanks,

Eduardo Arias
 
Last edited by a moderator:
it is adding the values:
Dim SubTotal As Double

SubTotal = SubTotal + ItemPrice

which translates to:

SubTotal = 0 + ItemPrice

because SubTotal is always disgarded when the program hits the "End Sub" line, everytime the user clicks the "Add" button SubTotal always equals 0

what you need to do is declare SubTotal at the top of the form, outside the button's click event:
VB.NET:
Private SubTotal As Double

Private Sub btnAdd_Click (...) Handles btnAdd.Click
    SubTotal += Item Price
    lblSubTotal.Text = SubTotal.ToString("c") 'the "c" part tells the ToString method to format the number as currency
End Sub
 
Back
Top