2016-08-02 78 views
1

你好,有人可以帮我在这里。我正在编写这个视觉基本功能,用于计算用户选定项目的价格。我已经使用了if if if循环,但只有第一部分显示输出,例如 我正在使用复选框进行选择,如果选中第一个复选框并单击了订单按钮,收据将填充价格和选定的项目,但这不会发生第二,第三...复选框。什么可能是错的?有人请帮助 下面是功能Visual basic If elseIf not working

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 

回答

1

只有您的代码只被选中CheckBox的逻辑将被执行。
CheckBox使用自己的If ... Then。这样你的代码将会检查每个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 
+0

其实这是我的第一个选择,但我避免了它,因为我需要检查是否有多个项目已被选中,它也没有工作。顺便说一下,你会介意告诉我,如果我的用法存在什么问题? –

+0

但我发现他们为什么都没有工作。这实际上是你提到的另一种方式,谢谢 –

2

复选框,ElseIf我的源代码?如果只有一个选择很重要,最好使用单选按钮而不是复选框。如果你还可以删除= True。另外请记住,如果一个If返回true,则其他ElseElseIf都不起作用。

+0

我用复选框,因为我想启用多项选择。其实我的问题是,只有如果评估为真,其他人甚至不应该(当他们检查时) –

+0

感谢人们,我已经解决了它。实际上问题出现在我调用函数的地方,我忘记了,并且留下了我的合作伙伴放置的前一个if else语句。 –