0

I would like some help with a system I was creating that has 15 CheckBox controls. The system is such that at one time all these CheckBox controls 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 CheckBox controls, I have several lines of code:

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
0

2 Answers 2

2

If you're a beginner, and your solution is working the way you want it to work, then there's nothing wrong with it. Other solutions I can think of may be considered 'more complicated' than what you're doing, but would certainly save you lines of code.

I'm not sure why your computeCurrentSelection() sub uses an ElseIf rather than simply using a separate If statement per checkbox. The way you have it here gives a strange priority system (for example, if chkugalis.Checked = True, then total will never include the calculations from the ElseIf chkSamosa.Checked = True line). And so, perhaps all you're looking for is to take out your call to computeCurrentSelection() and simply replace it with:

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")
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 " & total)
End If
'Etc

Though, if it were me coding a solution for accomplishing what (it looks like) you're trying to do, then I would do it this way:

  1. Create a Public Class MyCheckBox which Inherits Checkbox
  2. Add two new properties to it: Cost and ReceiptText
  3. Change the current checkbox controls to instead be of type MyCheckBox
  4. On the (form's?) load event, populate the new properties E.g.:

    chkugalis.Cost = 10.45
    chkugalis.ReceiptText = " plates of Ugali n fish "
  5. Add all relevant MyCheckBoxes to a collection of MyCheckBoxes

  6. Add a For Each mchkb In MyCheckBoxesCollection block to build your receipt. Something like:

For Each mchkb As MyCheckBox IN MyCheckBoxesCollection
    If mchkb.Checked = True Then
        orderAmt = lab.Text
        subtotal = mchkb.Cost * orderAmt
        total = total + subtotal
        lstReceipt.Items.Add(orderAmt & mchkb.ReceiptText & subTotal.ToString)
    End If
Next
lstReceipt.Items.Add("Total: " & total.ToString)
Sign up to request clarification or add additional context in comments.

Comments

1

Looking at your code I can see that it will only execute the code of the first checked CheckBox. Instead what you should do is separate your If statements:

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

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

....

lstReceipt.Items.Add("Total: " & total)

This way every CheckBox that is checked will have it's code executed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.