I'm doing a project that requires me to create a book ordering form. I have the books displayed in a listbox named bookselectionlistbox, and when a book is selected, it displays the price of that book in a label named bookpricelabel1. I have a button named addbutton that allows the user to add the book to the shopping cart. The shopping cart is in the form of a combobox named shoppingcartcombobox. Lastly, there is a receipt button that transfers ALL the books from the shopping cart to a seperate label called label3, not just the selected book. The last thing I need to do is have the price of each book also transfer to the receipt section. This needs to be done either on the same label3 on the same line as the book title, or on a seperate label that is adjacent to label3, I have temporarily placed a label there named label5. I am using a case statement for the bookselectionlistbox to say that when a certain book is selected, the bookpricelabel1 will display "$xx". I also have an IF statement under the addbutton label that generates a total price for all the books in the shopping cart, here is an example of the code I used for one of the books:

[Dim visualbasic As Decimal]
[If shoppingcartComboBox.Items.Contains("Visual Basic") Then]
[visualbasic = 55]
[Else]
[visualbasic = 0]
[End If]
[Me.bookpriceLabel2.Text = visualbasic + java + c + lannetworks + windowsnetworking +] [moreaboutnetworking + webprogramming + javascript + asp]

The code I used to transfer ALL the shoppingcartcombobox data to label3 is:
[Dim items As String]
[ For Each item As String In shoppingcartComboBox.Items]
[ items &= item & vbCrLf]
[ Next]
[ Label3.Text = items]

So I just need to populate the individual book prices now and I'm done, thanks in advance for any help, it's very appreciated. I've been stuck on this one part for a while and look forward to getting over it :)

Recommended Answers

All 4 Replies

Not quite clear. Could you provide just the needed info?

Member Avatar for Unhnd_Exception
Public Class Form1

    'Create a class to store the book info
    Private Class Book
        Private fpBookName As String
        Private fpBookPrice As Double

        Property BookName() As String
            Get
                Return fpBookName
            End Get
            Set(ByVal value As String)
                fpBookName = value
            End Set
        End Property

        Property BookPrice() As Double
            Get
                Return fpBookPrice
            End Get
            Set(ByVal value As Double)
                fpBookPrice = value
            End Set
        End Property

        Sub New(ByVal BookName As String, ByVal BookPrice As Double)
            fpBookName = BookName
            fpBookPrice = BookPrice
        End Sub

    End Class


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Set the display member to the book name.
        ListBoxBookSelection.DisplayMember = "BookName"
        ComboBoxShoppingCart.DisplayMember = "BookName"

        'Add two Books to the list box.  This will store the name and price
        'in the listbox but only display the name since the display member
        'is BookName
        ListBoxBookSelection.Items.Add(New Book("Peter's Big Adventure", 17.99))
        ListBoxBookSelection.Items.Add(New Book("Martha's Adventure with Peter", 18.99))

    End Sub

    Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
        'Validate that there is a selection in the listbox and
        'that the shopping cart doesn't contain the book.
        If ListBoxBookSelection.SelectedIndex = -1 Then Exit Sub
        If ComboBoxShoppingCart.Items.Contains(ListBoxBookSelection.SelectedItem) Then Exit Sub

        'Add the book to the shopping cart.  This will also add the name and
        'price to the combobox but only display the name.
        ComboBoxShoppingCart.Items.Add(ListBoxBookSelection.SelectedItem)

    End Sub

    Private Sub ButtonReceipt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReceipt.Click
        Dim Total As Double

        Label3.Text = String.Empty

        'Since the combobox is filled with book classes you can access
        'the book name and price straight from the combobox.
        For i = 0 To ComboBoxShoppingCart.Items.Count - 1
            'validate the item is a book.
            If ComboBoxShoppingCart.Items(i) Is Nothing OrElse Not TypeOf ComboBoxShoppingCart.Items(i) Is Book Then Continue For

            'Add the book name and price to the label.
            With CType(ComboBoxShoppingCart.Items(i), Book)
                If Not String.IsNullOrEmpty(Label3.Text) Then Label3.Text &= vbCrLf
                Label3.Text &= .BookName & "  " & .BookPrice
                Total += .BookPrice 'keep the total of all
            End With
        Next

        'Add the total to the label.
        If Not String.IsNullOrEmpty(Label3.Text) Then
            Label3.Text &= vbCrLf & "Total: " & Math.Round(Total, 2).ToString
        End If
    End Sub
End Class

I tried doing that but I couldn't get it to work after messing with it for a while. But I found 2 different methods I could get the book price added, I just need to figure out how to get it removed.
So now when the user selects a book from the bookselectionLISTBOX, it displays a price in bookpriceLABEL1. Then when they click the addBUTTON, LABEL5 (which is under the receipt section of my form) adds what is being displayed on the bookpriceLABEL1 & vbcrlf so it automatically drops to the next line of the label and continues each time a new book is added. Now I just need to add a code under the removeBUTTON property so when a book is removed from the shoppingcartCOMBOBOX, the books price is removed from LABEL5 as well.
Here are the 2 different codes I found I could use to add the book price to LABEL5:

Dim price As String
price &= bookpriceLabel1.Text & vbCrLf
Label5.Text &= price[

or

Label5.Text &= me.bookpriceLabel1.Text & vbCrLf

You probably need to use a For/Next loop.

Label1.Text = "" '// clear all from receipt.
        For Each bookAdded As String In ComboBox1.Items '// loop thru all items.
            '// check if Not empty, add newLine and book, else just add book.
            If Not Label1.Text = "" Then Label1.Text &= vbCrLf & bookAdded Else Label1.Text = bookAdded
        Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.