Implement a loop..

Yuengling

New member
Joined
Apr 28, 2010
Messages
1
Programming Experience
Beginner
To the code to stop the overwriting issue I'm having:

VB.NET:
 Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click

        If ComboBox1.Text = "" Then
            Return
        Else
            Quantity = Quantity + 1
            txtQuantity.Text = Quantity
        End If
    End Sub
________________________________________________________________
    Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click

        If ComboBox1.Text = "" Then
            Return
        Else
            Quantity = Quantity - 1
            txtQuantity.Text = Quantity
        End If
    End Sub
____________________________________________________________
If ComboBox1.SelectedIndex = 0 Then
            ListBox1.Items.Add(ComboBox1.SelectedItem & vbTab & " " & Quantity & " " & "@ €25.00" & vbTab & "Ready To Ship")
            DVDCost = DVDCost + FullMetalJacket
            Call CalculateTotalCost()
            TextBox2.Text = "Total Cost inc. VAT & Delivery" & vbTab & "€" & TotalCostIncDel
        End If
        If ComboBox1.SelectedIndex = 1 Then
            ListBox1.Items.Add(ComboBox1.SelectedItem & vbTab & " " & Quantity & " " & "@ €20.00" & vbTab & "Ready To Ship")
            DVDCost = DVDCost + AmericanHistoryX
            Call CalculateTotalCost()
            TextBox2.Text = "Total Cost inc. VAT & Delivery" & vbTab & "€" & TotalCostIncDel
        End If
        If ComboBox1.SelectedIndex = 2 Then
            ListBox1.Items.Add(ComboBox1.SelectedItem & vbTab & " " & Quantity & " " & "@ €9.99" & vbTab & "Ready To Ship")
            DVDCost = DVDCost + PublicEnemy
            Call CalculateTotalCost()
            TextBox2.Text = "Total Cost inc. VAT & Delivery" & vbTab & "€" & TotalCostIncDel
        End If
        If ComboBox1.SelectedIndex = 3 Then
            ListBox1.Items.Add(ComboBox1.SelectedItem & vbTab & " " & Quantity & " " & "@ €15.00" & vbTab & "Ready To Ship")
            DVDCost = DVDCost + Blow
            Call CalculateTotalCost()
            TextBox2.Text = "Total Cost inc. VAT & Delivery" & vbTab & "€" & TotalCostIncDel
        End If
        If ComboBox1.SelectedIndex < 0 And ComboBox1.SelectedIndex > 4 Then
            MessageBox.Show("Please Select A DVD", "Attention")
        End If
_________________________________________________________________

Function CalculateTotalCost() As Decimal
        TotalCostIncDel = DVDCost * Quantity
        Return TotalCostIncDel
    End Function


When I add single dvd's to the order it adds them fine and the total cost is correct but when i put the quantity of the dvd's to more than 1 its messes up the total cost.

E.G:
2873iuh.gif
 
From the screenshot you've provided, you would have to calculate the total cost by 1 * 25 + 3 * 20. Now look at the code in your CalculateTotalCost method. That code obviously doesn't do that. Ask yourself what the general case is: start with a total of 0 and, for each entry, multiple the count by the cost and add that to the running total. Now write some code to implement that. Obviously you want to do the same thing an unknown number of times. What does that suggest: a loop.
 
Back
Top