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

Vb.net processing multiple checkboxes

I would like some help with a system I was creating that has 15 check boxes, The system is such that at one time all these check boxes can be checked at the same time or at least five of them checked at the same time. I know how to handle it using

if else
but this seams tedious and will need too much coding. Could someone tell me if there is a better and simpler way of doing it.
Here is how I am doing it and even before beginning the multiple selections on the check boxes, I have several lines of code. Please help if anyone knows of an easier way. Thanks

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




End If
End Sub

Answer

Well I know that if you have the code set like this then the first checkbox it comes across that is checked will execute the code. Then it will end. It won't look into the next else so if chkugalis is checked and chkGitheri is checked it will only execute code for chkugalis. Your best bet would be to put them all into their own IF statements.

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

    If chkGitheri.Checked Then 'ugali dengu slected
        orderAmt = lab3.Text
        total = ugalid * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " plates of " & "Ugali n dengu " & total)
    End If

And so on. This way every checkbox that is checked will have it's code executed. You can also just use If chkugalis.Checked Then rather than If chkugalis.Checked = True Then. It's personal preference however I find it easier to read like this.