Hi guys,

I need help with some homework... hopefully this is the right forum.

So my last project assignment is making a shopping cart and having the user add, remove items therefrom. I am having a problem adjusting the totals after the items get removed from the cart: Using Lists to show items in cart.

Structure Item
        Public strItemName As String
        Public dblPrice As Double
    End Structure

    Public Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
        itmItem(0).strItemName = "The War"
        itmItem(0).dblPrice = 10
        itmItem(1).strItemName = "Radio One"
        itmItem(1).dblPrice = 15


        While intNum <= 2
            lstItems.Items.Add(itmItem(intNum).strItemName)
            intNum += 1
        End While

    End Sub

    Private Sub lstItems_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstItems.SelectedIndexChanged
        intSelected = lstItems.SelectedIndex
        lblPrice.Text = itmItem(intSelected).dblPrice.ToString("C2")
    End Sub

    Public Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

        Dim intToCart As Integer = lstItems.SelectedIndex


        lstCart.Items.Add(lstItems.Items(intToCart))
        dblSubtotal += itmItem(intToCart).dblPrice

        dblTax = dblSubtotal * dblTAR_RATE
        dblTotal = dblSubtotal + dblTax
        lblSubtotal.Text = dblSubtotal.ToString("C2")
        lblTax.Text = dblTax.ToString("C2")
        lblTotal.Text = dblTotal.ToString("C2")

    End Sub

    Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        Dim intIndex As Integer = lstCart.SelectedIndex
        lstCart.Items.RemoveAt(intIndex)
        End Sub

I ma havng a hard time defining the "names" that are already in "lstCart" as items (with a name and price property, this way I can just subtract the price property if the "removed item" from the total), the name being the same as the one in the lstCart list.

Any ideas would be welcome.

rnyamonga
Deleted Member

You might need to rethink your approach a bit. The ListBox class already has an item collection it's called Items. It has everything you need to maintain the shopping cart. You can use a list for the available items. I would suggest overriding the ToString() function of the Item structure to give a formatted output to the listbox:

Public Structure Item
    Public strItemName As String
    Public dblPrice As Double
    Public Overrides Function ToString() As String
        Return String.Format("{0}{1:C}", {strItemName.PadRight(30) & vbTab, dblPrice})
    End Function
End Structure

Now whenever you add an item the listbox will call the tostring function to display the data. Since every item in the listbox is in reality an Item getting the data from a selected item is simply a matter of using the DirectCast function:

 Dim chosenItem As Item = DirectCast(ListBox1.SelectedItem, Item)

This now allows you to make all of the different totals Item's that you can manipulate everytime you add or remove an item from the cart.

Edited 1 Year Ago by tinstaafl

Comments
Nice supporting

@tinstaafl: Great illustration about structure and also awasome tutorial for n'bees. Truely great. Thank u.

@rnyamonga:
Referencing and using tinstaafl's supporting, I want to show you, how could you add and remove items from listbox object.

Public Class Form1

    Public Structure Item
        Public strItemName As String
        Public dblPrice As Double
        Public Overrides Function ToString() As String
            Return String.Format("{0}{1:C}", {strItemName.PadRight(30) & vbTab, dblPrice})
        End Function
    End Structure

    Dim itmItem(1) As Item
    Dim intNum As Integer = 0

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        itmItem(0).strItemName = "The War"
        itmItem(0).dblPrice = 10
        itmItem(1).strItemName = "Radio One"
        itmItem(1).dblPrice = 15


        While intNum <= 2
            'Adding itemsto the listbox
            lstItem.Items.Add(itmItem(intNum).ToString())
            intNum += 1
        End While
    End Sub

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click

        If Not IsNothing(lstItem.SelectedItem) Then
            'adding items to the cart listboox and calculate total amount
            lstCart.Items.Add(lstItem.Items(lstItem.SelectedIndex))

            lblText.Text = Val(lblText.Text) + itmItem(lstItem.SelectedIndex).dblPrice

            lstItem.SelectedIndex = -1

        Else
            MessageBox.Show("Please select an Item from list.")
        End If

    End Sub



    Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click


        If Not IsNothing(lstCart.SelectedItem) Then

            Dim x() As String = Split(lstCart.Items(lstCart.SelectedIndex), vbTab)

            For i As Integer = 0 To itmItem.Count - 1

                If itmItem(i).strItemName = Trim(x(0)) Then

                    'tring to subtract the value
                    lblText.Text = Val(lblText.Text) - itmItem(i).dblPrice

                    Exit For

                End If
            Next

            'remove item fro cart
            lstCart.Items.Remove(lstCart.SelectedItem)
            lstCart.SelectedIndex = -1

        Else
            MessageBox.Show("Please select an Item from list.")
        End If


    End Sub



End Class

Hope it can help you.

I had to think about this all day, but here's what I ended up doing:

Public Function GetPrice(ByVal strItmName As String) As Double
        Dim i As Integer
        Dim dblItmPrice As Double
        itmItem(0).strItemName = "The War"
        itmItem(0).dblPrice = 10
        itmItem(1).strItemName = "Radio One"
        itmItem(1).dblPrice = 15 

        While i <= 9            
            If strItmName Like itmItem(i).strItemName Then
                dblItmPrice = itmItem(i).dblPrice
            End If
            i += 1
        End While

        Return dblItmPrice
    End Function

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
        Dim dblPriceInCart As Double
        Dim intIndex As Integer = lstCart.SelectedIndex

        dblPriceInCart = GetPrice(lstCart.SelectedItem.ToString)
        dblSubtotal -= dblPriceInCart
        dblTax = dblSubtotal * dblTAR_RATE
        dblTotal = dblSubtotal + dblTax + intShip

        lblSubtotal.Text = dblSubtotal.ToString("C2")
        lblTax.Text = dblTax.ToString("C2")
        lblTotal.Text = dblTotal.ToString("C2")

    End Sub

End Class

Thanks for all the ideas duys, really appreciate.

This question has already been answered. Start a new discussion instead.