Results 1 to 7 of 7

Thread: Adding a price to a listbox from a item from another listbox...

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    10

    Adding a price to a listbox from a item from another listbox...

    Getting stuck on this I know it's simple and maybe I'm just over thinking it...Help is much appreciated.

    In the CLICK events for the Add buttons on both the Audio Books and Print Books forms, add the selected item to the Shopping Cart listbox on the main form and add its corresponding price to the Costs listbox on the main form.

    I've tried multiple things but at a loss...


    Code:
    Option Strict On
    
    Public Class Form1
    
    
        Public Const decSALES_TAX As Decimal = 0.06D
        Public Const decSHIPPING_CHARGE As Decimal = 2
    
        Public printBookPrice() As Decimal = {11.95D, 14.5D, 29.95D, 18.5D}
        Public printBookName() As String = {"I Did It your Way", "The History of Scotland", "Learn Calculus in One Day", "Feel the Stress"}
        Public audioBookPrice() As Decimal = {29.95D, 14.5D, 12.95D, 11.5D}
        Public audioBookName() As String = {"Learn Calculus in One Day", "The History of Scotland", "The Science of Body Language", "Relaxation Techniques"}
    
        Private Sub mnuReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuReset.Click
            lblSub.Text = String.Empty
            lblTax.Text = String.Empty
            lblShipp.Text = String.Empty
            lblTotal.Text = String.Empty
            lstboxCart.Items.Clear()
        End Sub
    
        Private Sub mnuPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrint.Click
            frmPrintBooks.Show()
        End Sub
    
        Private Sub mnuAudio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAudio.Click
            frmAudioBooks.Show()
        End Sub
    
        Public Sub DisplayCharges()
            Dim BookTotal As Decimal = 0
            For Each Book As String In lstboxCart.Items
                If Book.StartsWith("A") Then
                    BookTotal += audioBookPrice(CInt(Book.Substring(1, 1)))
                Else
                    BookTotal += printBookPrice(CInt(Book.Substring(1, 1)))
                End If
            Next
            Dim BookCount As Integer = lstboxCart.Items.Count
            lblSub.Text = BookTotal.ToString("C2")
            lblTax.Text = (BookTotal * decSALES_TAX).ToString("C2")
            BookTotal += BookTotal * decSALES_TAX
            lblShipp.Text = (BookCount * decSHIPPING_CHARGE).ToString("C2")
            BookTotal += BookCount * decSHIPPING_CHARGE
            lblTotal.Text = BookTotal.ToString("C2")
    
        End Sub
    
    
        Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
            Me.Close()
        End Sub
    
    
        Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
            MessageBox.Show("The Shopping Cart System allows customers to shop for both Print and Audio Books.", "Shopping Cart About")
        End Sub
    
        Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
            If lstboxCart.SelectedIndex <> -1 Then
                lstboxCart.Items.RemoveAt(lstboxCart.SelectedIndex)
            End If
            DisplayCharges()
        End Sub
    
    
    End Class
    Code:
    Option Strict On
    
    Public Class frmAudioBooks
    
        Private Sub btnCloseaudio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Me.Close()
        End Sub
    
        Private Sub btnAddaudio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddprint.Click
            Form1.lstboxCart.Items.Add(lstAudiobooks.SelectedIndex & ": " & lstAudiobooks.SelectedItem.ToString)
            Form1.DisplayCharges()
        End Sub
    
        Private Sub frmAudioBooks_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For x = 0 To UBound(Form1.printBookName)
                lstAudiobooks.Items.Add(Form1.printBookName(x) & " " & String.Format("{0:C}", Form1.printBookPrice(x)))
            Next x
        End Sub
    
    
    End Class
    Code:
    Option Strict On
    
    Public Class frmPrintBooks
    
        Private Sub btnCloseprint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Me.Close()
        End Sub
    
        Private Sub btnAddprint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            Form1.lstboxCart.Items.Add(lstPrintbooks.SelectedIndex & ": " & lstPrintbooks.SelectedItem.ToString)
            Form1.DisplayCharges()
        End Sub
    
        Private Sub frmPrintbooks_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For x = 0 To UBound(Form1.printBookName)
                lstPrintbooks.Items.Add(Form1.printBookName(x) & " " & String.Format("{0:C}", Form1.printBookPrice(x)))
            Next x
        End Sub
    End Class
    Last edited by shadbehnke; Apr 13th, 2014 at 04:11 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Adding a price to a listbox from a item from another listbox...

    when you post code, use [Code]'your code here[/Code] tags + it'll format like this:

    Code:
    'your code here

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    10

    Re: Adding a price to a listbox from a item from another listbox...

    Alright got that still no help with the code though lol

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Adding a price to a listbox from a item from another listbox...

    ok. there are several errors, with the most important being you're supposed to have a Costs listbox, which isn't mentioned in your code.

    your frmAudioBooks needs modifying:

    Code:
    Public Class frmAudioBooks
    
        Private Sub btnCloseaudio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Me.Close()
        End Sub
    
        Private Sub btnAddaudio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddaudio.Click
            Form1.lstboxCart.Items.Add(lstAudiobooks.SelectedIndex & ": " & lstAudiobooks.SelectedItem.ToString)
            Form1.DisplayCharges()
        End Sub
    
        Private Sub frmAudioBooks_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For x = 0 To UBound(Form1.audioBookName)
                lstAudiobooks.Items.Add(Form1.audioBookName(x) & " " & String.Format("{0:C}", Form1.audioBookPrice(x)))
            Next x
        End Sub
    
    End Class
    then your DisplayCharges sub:

    Code:
    Public Sub DisplayCharges()
        Dim BookTotal As Decimal = 0
        For Each Book As String In lstboxCart.Items
            Dim bookName As String = Book.Substring(Book.IndexOf(":") + 2, Book.IndexOf("£") - 4)
            If audioBookName.Contains(bookName) Then
                BookTotal += audioBookPrice(CInt(Val(Book)))
            Else
                BookTotal += printBookPrice(CInt(Val(Book)))
            End If
        Next
        Dim BookCount As Integer = lstboxCart.Items.Count
        lblSub.Text = BookTotal.ToString("C2")
        lblTax.Text = (BookTotal * decSALES_TAX).ToString("C2")
        BookTotal += BookTotal * decSALES_TAX
        lblShipp.Text = (BookCount * decSHIPPING_CHARGE).ToString("C2")
        BookTotal += BookCount * decSHIPPING_CHARGE
        lblTotal.Text = BookTotal.ToString("C2")
    
    End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    10

    Re: Adding a price to a listbox from a item from another listbox...

    I do have a costs listbox. So after implementing your code I come up with this error...Kind of hard to say but it says value cannot be less then zero. Sorry kind of a newb...

    Name:  Screen.jpg
Views: 86
Size:  35.6 KB

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Adding a price to a listbox from a item from another listbox...

    is the value in Book in this format (as your original code suggests):

    0: Learn Calculus in One Day £29.95
    0: I Did It your Way £11.95

    ???

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Adding a price to a listbox from a item from another listbox...

    it's probably this:

    Book.IndexOf("£")

    change it to your local currency symbol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width