Vb.net VB-如果只选中一定数量的复选框,是否运行if语句?
我正在为我的VB初级课程创建一个比萨饼订购系统。我可以为比萨编码一种配料,但当我尝试添加一种以上的配料时,我会得到两种配料的比萨,还有两种只有一种配料的比萨。如果在“组”框中仅选择了一定数量的复选框,是否有方法运行if语句Vb.net VB-如果只选中一定数量的复选框,是否运行if语句?,vb.net,checkbox,groupbox,Vb.net,Checkbox,Groupbox,我正在为我的VB初级课程创建一个比萨饼订购系统。我可以为比萨编码一种配料,但当我尝试添加一种以上的配料时,我会得到两种配料的比萨,还有两种只有一种配料的比萨。如果在“组”框中仅选择了一定数量的复选框,是否有方法运行if语句 Private Sub AddItems() 'Declare Topping Variables Dim topping1 As String = "Pepperoni" Dim topping2 As String = "Bacon" D
Private Sub AddItems()
'Declare Topping Variables
Dim topping1 As String = "Pepperoni"
Dim topping2 As String = "Bacon"
Dim topping3 As String = "Ham"
'Declare Size Variables
Dim strPersonal As String = "Persoanl"
Dim strSmall As String = "Small"
Dim strMedium As String = "Medium"
Dim strLarge As String = "Large"
Dim strExLarge As String = "Extra Large"
'Personal Single Item
If radPersonal.Checked = True And chkPepperoni.Checked = True Then
CheckedListBox1.Items.Add(strPersonal & " with " & topping1)
End If
If radPersonal.Checked = True And chkBacon.Checked = True Then
CheckedListBox1.Items.Add(strPersonal & " with " & topping2)
End If
If radPersonal.Checked = True And chkHam.Checked = True Then
CheckedListBox1.Items.Add(strPersonal & " with " & topping3)
End If
'Personal Two Items
If radPersonal.Checked = True And chkPepperoni.Checked = True And chkBacon.Checked = True Then
CheckedListBox1.Items.Add(strPersonal & " with " & topping1 & " and " & topping2)
End If
If radPersonal.Checked = True And chkBacon.Checked = True And chkHam.Checked = True Then
CheckedListBox1.Items.Add(strPersonal & " with " & topping2 & " and " & topping3)
End If
If radPersonal.Checked = True And chkHam.Checked = True And chkPepperoni.Checked = True Then
CheckedListBox1.Items.Add(strPersonal & " with " & topping3 & " and " & topping1)
End If
End Sub
首先,你不应该测试
radPersonal。检查了六次。您应该只测试一次,然后嵌套其余部分
其次,您不应该单独测试,例如,chkpeperoni.Checked
,然后再组合测试。如果您这样做,那么,如果它被组合检查,它将匹配两个测试。您应该首先测试组合,然后仅在组合不匹配时测试它。我可能倾向于这样做:
If radPersonal.Checked Then
If chkPepperoni.Checked Then
If chkBacon.Checked Then
'pepperoni and bacon
ElseIf chkHam.Checked Then
'pepperoni and ham
Else
'pepperoni only
End If
ElseIf chkBacon.Checked Then
If chkHam.Checked Then
'bacon and ham
Else
'bacon only
End If
ElseIf chkHam.Checked Then
'ham only
End If
End If