The Only Smart Boy The Only Smart Boy - 4 months ago 17
Vb.net Question

Visual basic If elseIf not working

Hello could someone please help me here. I am writing this visual basic function that for computing the price of selected items by a user. I have used an if else If loop but only the first part displays the output for example
I am using check boxes for selection, if the first checkbox is selected and the order button clicked, the receipt is populated with the price and selected item but this does not happen for the second, third ... check Boxes. What could be wrong? Someone please help
Here is my source code for the function

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

Answer

With your code only first selected CheckBox's logic will be executed.
Use own If ... Then for every CheckBox. In that way your code will checks every CheckBox.

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

    If chkUgaliB.Checked = True Then 'githeri selected
        orderAmt = lab2.Text
        total = githeri * orderAmt
        lstReceipt.Items.Add(orderAmt & " plates of " & "Githeri " & total)
    End If

    ' Other Ifs

End Sub