Click here to Skip to main content
15,301,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello im trying to resolve a issue im having with my code. i want my second listbox to display a subtotal of all the items extended prices from my first listbox. i cant figoure out how to get the subtotal. please help im new to this so my code may be messy. when i add the items to my first listbox i cant get the extended price of each as a subtotal to my second listbox.



VB
Public Class Form1
    Dim extendedprice As Decimal
    Dim subtotal As Decimal


    Private Sub btnorder_Click(sender As Object, e As EventArgs) Handles btnorder.Click
        Dim itemnumber As Decimal
        Dim price As Decimal
        Dim quantity As Decimal

        itemnumber = txtitemnumber.Text
        quantity = txtquantity.Text
        extendedprice = price * quantity

        'Item Number 100 displays Wrench
        If itemnumber = 100 Then
            extendedprice = 3.5 * quantity
            listorder.Items.Add("Item Number: 100")
            listorder.Items.Add("Description: Wrench")
            listorder.Items.Add("Quantity:" & quantity)
            listorder.Items.Add("Price: $3.50")
            listorder.Items.Add("Extended Price:" & extendedprice.ToString("c"))

        End If

        'Item Number 200 displays Pipe Wrench
        If itemnumber = 200 Then
            extendedprice = 5.75 * quantity
            listorder.Items.Add("Item Number: 200")
            listorder.Items.Add("Description: Pipe Wrench")
            listorder.Items.Add("Quantity:" & quantity)
            listorder.Items.Add("Price: $5.75")
            listorder.Items.Add("Extended Price:" & extendedprice.ToString("c"))
        End If

        'Item Number 300 displays Rip Saw
        If itemnumber = 300 Then
            extendedprice = 16.23 * quantity
            listorder.Items.Add("Item Number: 300")
            listorder.Items.Add("Description: Rip Saw")
            listorder.Items.Add("Quantity:" & quantity)
            listorder.Items.Add("Price: $16.23")
            listorder.Items.Add("Extended Price:" & extendedprice.ToString("c"))
        End If

        'Item Number 400 displays Framing Hammer
        If itemnumber = 400 Then
            extendedprice = 32.5 * quantity
            listorder.Items.Add("Item Number: 400")
            listorder.Items.Add("Description: Framing Hammer")
            listorder.Items.Add("Quantity:" & quantity)
            listorder.Items.Add("Price: $32.50")
            listorder.Items.Add("Extended Price:" & extendedprice.ToString("c"))
        End If

        'Item Number 500 displays Square
        If itemnumber = 500 Then
            extendedprice = 27.5 * quantity
            listorder.Items.Add("Item Number: 500")
            listorder.Items.Add("Description: Square")
            listorder.Items.Add("Quantity:" & quantity)
            listorder.Items.Add("Price: $27.50")
            listorder.Items.Add("Extended Price:" & extendedprice.ToString("c"))
        End If

        'Item Number 600 displays Solder
        If itemnumber = 600 Then
            extendedprice = 6.34 * quantity
            listorder.Items.Add("Item Number: 600")
            listorder.Items.Add("Description: Solder")
            listorder.Items.Add("Quantity:" & quantity)
            listorder.Items.Add("Price: $6.34")
            listorder.Items.Add("Extended Price:" & extendedprice.ToString("c"))
        End If

        'Item Number 700 displays Paste
        If itemnumber = 700 Then
            extendedprice = 4.26 * quantity
            listorder.Items.Add("Item Number: 700")
            listorder.Items.Add("Description: Paste")
            listorder.Items.Add("Quantity:" & quantity)
            listorder.Items.Add("Price: $4.26")
            listorder.Items.Add("Extended Price:" & extendedprice.ToString("c"))
        End If

        'Item Number 800 displays Screwdriver
        If itemnumber = 800 Then
            extendedprice = 11.77 * quantity
            listorder.Items.Add("Item Number: 800")
            listorder.Items.Add("Description: Screwdriver")
            listorder.Items.Add("Quantity:" & quantity)
            listorder.Items.Add("Price: $11.77")
            listorder.Items.Add("Extended Price:" & extendedprice.ToString("c"))
        End If

        ' Adds a line under the order
        listorder.Items.Add("--------------------------------------------")

        'Clears the Item Number Text Box & Quantity Text Box
        txtitemnumber.Text = ""
        txtquantity.Text = ""


    End Sub

    Private Sub btncheckout_Click(sender As Object, e As EventArgs) Handles btncheckout.Click
        Dim discount As Decimal
        Dim totalprice As Decimal


        discount = extendedprice - discount
        totalprice = extendedprice * discount




        listinvoice.Items.Add("Timber Tom’s Hardware")
        listinvoice.Items.Add(Date.Now)
        listinvoice.Items.Add("--------------------------------------------")




        listinvoice.Items.Add("Sub Total " & subtotal.ToString("c"))


        'no discount
        If rbnodiscount.Checked Then
            totalprice = extendedprice
            listinvoice.Items.Add("No Discount")
            listinvoice.Items.Add("Total Price:" & totalprice.ToString("c"))
        End If

        '10% discount
        If rb10discount.Checked Then
            discount = extendedprice * 0.1
            totalprice = extendedprice - discount
            listinvoice.Items.Add("10%Discount:" & discount.ToString("c"))
            listinvoice.Items.Add("Total Price:" & totalprice.ToString("c"))
        End If


        '15% discount
        If rb15discount.Checked Then
            discount = extendedprice * 0.15
            totalprice = extendedprice - discount
            listinvoice.Items.Add("15% Discount:" & discount.ToString("c"))
            listinvoice.Items.Add("Total Price:" & totalprice.ToString("c"))
        End If




    End Sub

    Private Sub btnclearitems_Click(sender As Object, e As EventArgs) Handles btnclearitems.Click

        'Clears Order List Box
        listorder.Items.Clear()

    End Sub

    Private Sub btnclearinvoice_Click(sender As Object, e As EventArgs) Handles btnclearinvoice.Click

        'Clears Invoice List Box
        listinvoice.Items.Clear()

    End Sub

    Private Sub btnexit_Click(sender As Object, e As EventArgs) Handles btnexit.Click

        'Asks if you want to exit
        If MsgBox("Are you sure you want to exit?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
            Application.Exit()

        End If
    End Sub
End Class


What I have tried:

i have tryed different ways but im lost on this last part of it ;(
Posted
Updated 3-Apr-17 4:37am
v2
Comments
[no name] 23-Oct-16 20:48pm
   
Strings are not numbers. You can't multiply strings and numbers together.
Suvendu Shekhar Giri 23-Oct-16 23:07pm
   
Do you know debugging?

1 solution

Assuming you still have to complete this assignment...

You must convert the contents of the textboxes to the appropriate type before attempting to use them in any calculation:
VB
Dim itemnumber As Integer
Dim quantity As Decimal

If Not Integer.TryParse(txtitemnumber.Text, itemnumber) Then
	MessageBox.Show("No item number supplied")
	Return
End If
If Not Decimal.TryParse(txtquantity.Text, quantity) Then
	MessageBox.Show("No Quantity supplied")
	Return
End If
Then in your checkout button you are assuming that there is only one item on the invoice and extendedprice will only be assigned a value once. Better would be to go through the invoice and calculate the values required - I'm using your listview here, it's not the best approach but it will do to demonstrate.
VB
Private Sub btncheckout_Click(sender As Object, e As EventArgs) Handles btncheckout.Click

	Dim discount As Decimal

	'no discount
	If rbnodiscount.Checked Then
		discount = 0
	End If
	'10% discount
	If rb10discount.Checked Then
		discount = 10
	End If
	'15% discount
	If rb15discount.Checked Then
		discount = 15
	End If

	Dim subTotal As Decimal = 0.0
	For Each item In listorder.Items
		If item.ToString().StartsWith("Extended Price:") Then
			Dim extendedprice As Decimal = Decimal.Parse(item.ToString().Substring(16))
			subTotal = subTotal + extendedprice
		End If
	Next

	Dim totalprice As Decimal = subTotal - ((discount / 100.0) * subTotal)

	listinvoice.Items.Add("Timber Tom’s Hardware")
	listinvoice.Items.Add(Date.Now)
	listinvoice.Items.Add("--------------------------------------------")

	listinvoice.Items.Add("Sub Total " & subtotal.ToString("c"))
	listinvoice.Items.Add(String.Format("{0} Discount", IIf(discount = 0, "No", discount.ToString() + "%")))
	listinvoice.Items.Add("Total Price:" & totalprice.ToString("c"))

End Sub
Your next step should be to move all of that information about the Items into a collection - separate your data from your presentation
   
Comments
Graeme_Grant 3-Apr-17 10:38am
   
Nice answer for an old question ;)
CHill60 3-Apr-17 10:41am
   
Not sure how it ended up at the top of my list (washed my mouse finger and now can't do a thing with it probably), but I reckoned that they must be nearing their end-of-term exams by now ;)
Graeme_Grant 3-Apr-17 11:30am
   
Happens to all of us :)

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