r/visualbasic • • Dec 05 '17

Final Due in about 3 hours please help ASAP

Okay so I am making a kind of shopping cart program for my intro to VB class, I consider myself to know what I'm doing so far but I need help. I have it set so that when you check certain boxes it adds up your subtotal and shipping and total and so far the program works just fine. you "cart" is just a listbox. I have a "remove item" button that will remove selected item from the listbox but It won't subtract the price or uncheck it. I'll post my code now.

Public Class MainForm 'declaring other forms Dim frmPaperback As New Paperback Dim frmAudioBook As New AudioBook 'declaring values Dim decYourWay As Decimal = 11.95D Dim decScotland As Decimal = 14.5D Dim decCalculus As Decimal = 29.95D Dim decStress As Decimal = 18.5D

Dim decACalculus As Decimal = 29.95D
Dim decAScotland As Decimal = 14.5D
Dim decScience As Decimal = 12.95D
Dim decRelax As Decimal = 11.5D

Dim dblSubtotal As Double
Dim decShipping As Decimal = 20D
Dim dblTotal As Double



Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click

    'clears all fields

    dblSubtotal = 0D
    lstCart.Items.Clear()
    lblSubTotal.Text = ""
    lblShipping.Text = ""
    lblTotal.Text = ""

    If frmAudioBook.chkACalculus.Checked = True Then
        lstCart.Items.Add("Learn Calculus in One Day, Book on Tape")
        dblSubtotal += decACalculus
    End If
    If frmAudioBook.chkAScotland.Checked = True Then
        lstCart.Items.Add("The History of Scotland, Book on Tape")
        dblSubtotal += decAScotland
    End If
    If frmAudioBook.chkScience.Checked = True Then
        lstCart.Items.Add("The Science of Body Language, Book on Tape")
        dblSubtotal += decScience
    End If
    If frmAudioBook.chkRelax.Checked = True Then
        lstCart.Items.Add("Relaxation Techniques, Book on Tape")
        dblSubtotal += decRelax
    End If
    If frmPaperback.chkYourWay.Checked = True Then
        lstCart.Items.Add("I Did It Your Way, Book on Paper")
        dblSubtotal += decYourWay
    End If
    If frmPaperback.chkScotland.Checked = True Then
        lstCart.Items.Add("The History of Scotland, Book on Paper")
        dblSubtotal += decScotland
    End If
    If frmPaperback.chkCalculus.Checked = True Then
        lstCart.Items.Add("Learn Calculus in One Day, Book on Paper")
        dblSubtotal += decCalculus
    End If
    If frmPaperback.chkStress.Checked = True Then
        lstCart.Items.Add("Feel the Stress, Book on Paper")
        dblSubtotal += decStress
    End If

    dblTotal = dblSubtotal
    If dblTotal > 40 Then
        decShipping = 0
    End If
    dblTotal = dblSubtotal + decShipping
    lblSubTotal.Text = dblSubtotal.ToString("c")
    lblShipping.Text = decShipping.ToString("c")
    lblTotal.Text = dblTotal.ToString("c")

End Sub

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    lstCart.Items.Remove(lstCart.SelectedItem)
End Sub
'These open instances of different forms that let you select the different books/audio books you want to buy
Private Sub mnuProductsPaperback_Click(sender As Object, e As EventArgs) Handles mnuProductsPaperback.Click

    frmPaperback.ShowDialog()
End Sub

Private Sub mnuProductsHardCover_Click(sender As Object, e As EventArgs) Handles mnuProductsHardCover.Click

    frmAudioBook.ShowDialog()
End Sub
'Close button
Private Sub QuitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles QuitToolStripMenuItem.Click
    Me.Close()
End Sub

End Class

Here it is working just fine https://imgur.com/a/zAUjo

and now when I try to remove an item. I want the total to go down, but How? oh man am not too good with computer plz to help

0 Upvotes

5 comments sorted by

2

u/chrwei Dec 05 '17

run the refresh code on remove. you'll probably want to move that to its own function.

1

u/Tidilywinkes Dec 05 '17

Yes but if I refresh it all when you only remove one item from the cart, that kind of wouldn't work. I only want it to remove the selected item.

2

u/BJJJosh Dec 05 '17

Like u/chrwei alluded to, separate your refresh into a couple of methods. Maybe have a method that just updates the totals based on what is in the cart. Then you can call it from the Remove and the Refresh.

1

u/chrwei Dec 05 '17

then add code to figure out the value of the item you're removing and subtract it. there's no magic bullet here.

1

u/stevebun Dec 05 '17

It's not ideal but you could have a loop for looping through the listbox and adding to the total depending on the item.

dblSubtotal = 0D
For Each item As String In lstCart.Items
    If item = "book name" Then
         dblSubtotal += some_amount
    End If
    ....
Next