The Only Smart Boy The Only Smart Boy - 1 month ago 6
Vb.net Question

Calculating the sub total from a list of check boxes in vb.net

Hello I have written the following codes to handle the selection of check boxes in a form. the grand total should be incremented based on the slections and then displayed. Would someone please help me on how to ago about with this. Here is how I am processing the selections and trying to calculate the grand total.

Private Sub computeCurrentSelection()
If chkugalis.Checked = True Then 'ugali fish selected
orderAmt = lab.Text
total = ugalif * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " plates of" & " Ugali n fish " & " Kshs" & total)
End If

If chkGitheri.Checked = True Then 'ugali dengu slected
orderAmt = lab3.Text
total = ugalid * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " plates of " & "Ugali n dengu " & " Kshs" & total)
End If
If chkUgaliB.Checked = True Then 'githeri selected
orderAmt = lab2.Text
total = githeri * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " plates of " & "Githeri " & " Kshs" & total)
End If
If chkPilau.Checked = True Then
orderAmt = lab4.Text
total = chapo * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " plates of " & "Pilau " & " Kshs" & total)
End If
If chkPizza.Checked = True Then
orderAmt = lab5.Text
total = pilau * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " plates of " & "Pizza " & " Kshs" & total)
End If
If chkMandazi.Checked = True Then
orderAmt = lab6.Text
total = pizza * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & "mandazi " & " Kshs" & total)
End If
If chkSamosa.Checked = True Then
orderAmt = lab7.Text
total = mandazi * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & "Samosa " & " Kshs" & total)
End If
If chkChapon.Checked = True Then
orderAmt = lab8.Text
total = samosa * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & "Chapati " & " Kshs" & total)
End If
If chkWater.Checked = True And chk300ml.Checked = True Then
orderAmt = lab9.Text
total = water1 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 300ml Water " & " Kshs" & total)
End If
If chkWater.Checked = True And chk500ml.Checked = True Then
orderAmt = lab9.Text
total = water2 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 500ml Water " & " Kshs" & total)
ElseIf chkWater.Checked = True And chk1l.Checked = True Then
orderAmt = lab9.Text
total = water3 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 1l Water " & " Kshs" & total)
End If
If chkWater.Checked = True And chk2l.Checked = True Then
orderAmt = lab9.Text
total = water4 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 2l Water " & " Kshs" & total)
End If
If chkSoda.Checked = True And chk300ml.Checked = True Then
orderAmt = lab10.Text
total = soda1 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 300ml Soda " & " Kshs" & total)
End If
If chkSoda.Checked = True And chk500ml.Checked = True Then
orderAmt = lab10.Text
total = soda2 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 500ml Soda " & " Kshs" & total)
End If
If chkSoda.Checked = True And chk1l.Checked = True Then
orderAmt = lab10.Text
total = soda3 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 1l Soda " & " Kshs" & total)
End If
If chkSoda.Checked = True And chk2l.Checked = True Then
orderAmt = lab10.Text
total = soda4 * orderAmt
lstReceipt.Items.Add(orderAmt & " Bottles of 2l Soda " & " Kshs" & total)
End If
If chkJuice.Checked = True And chk300ml.Checked = True Then
orderAmt = lab11.Text
total = juice1 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 300ml juice " & " Kshs" & total)
End If
If chkJuice.Checked = True And chk500ml.Checked = True Then
orderAmt = lab11.Text
total = juice2 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 500ml juice " & " Kshs" & total)
End If
If chkJuice.Checked = True And chk1l.Checked = True Then
orderAmt = lab11.Text
total = juice3 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 1l juice " & " Kshs" & total)
End If
If chkJuice.Checked = True And chk2l.Checked = True Then
orderAmt = lab11.Text
total = juice4 * orderAmt
subtotal = total
lstReceipt.Items.Add(orderAmt & " Bottles of 2l juice " & " Kshs" & total)
End If
End Sub

Answer

I'm not sure if I understood correctly but I will give it a go.

Try this:

Private Function ComputeCurrentSelection() As Integer ' not sure for data type, you decide
    Dim total As Integer
    If chkugalis.Checked = True Then 'ugali fish selected
        orderAmt = lab.Text
        total += ugalif * orderAmt        
        lstReceipt.Items.Add(orderAmt & " plates of" & " Ugali n fish " & " Kshs" & total)
    End If

    If chkGitheri.Checked = True Then 'ugali dengu slected
        orderAmt = lab3.Text
        total += ugalid * orderAmt
        lstReceipt.Items.Add(orderAmt & " plates of " & "Ugali n dengu " & " Kshs" & total)
    End If
    If chkUgaliB.Checked = True Then 'githeri selected
        orderAmt = lab2.Text
        total += githeri * orderAmt
        lstReceipt.Items.Add(orderAmt & " plates of " & "Githeri " & " Kshs" & total)
    End If ...

    Return total
End Function