处理多个CheckBox控件

时间:2016-10-11 14:14:21

标签: vb.net checkbox

我想帮助我创建的系统有15个CheckBox控件。系统是这样的,一次可以同时检查所有这些CheckBox控件,或者同时检查其中至少五个。

我知道如何使用If[...]Else来处理它,但这种接缝繁琐且需要太多编码。

有人可以告诉我是否有更好更简单的方法吗?

以下是我的工作方式,甚至在开始对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")
    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

2 个答案:

答案 0 :(得分:3)

如果您是初学者,并且您的解决方案按照您希望的方式运行,那么它没有任何问题。我能想到的其他解决方案可能会被认为是更复杂的问题。比你正在做的事情,但肯定会保存你的代码行。

我不确定为什么您的computeCurrentSelection()子使用ElseIf而不是简单地为每个复选框使用单独的If语句。你在这里的方式给出了一个奇怪的优先级系统(例如,如果chkugalis.Checked = True,那么total将永远不会包含来自ElseIf chkSamosa.Checked = True行的计算。所以,您可能正在寻找的只是将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
    subtotal = total
    lstReceipt.Items.Add(orderAmt & " plates of " & "Ugali n dengu " & total)
End If
'Etc

但是,如果是我编写了一个解决方案来完成你想要做的事情(看起来像),那么我会这样做:

  1. 创建Public Class MyCheckBox Inherits Checkbox
  2. 向其添加两个新属性:CostReceiptText
  3. 将当前的checkbox控件更改为MyCheckBox
  4. 类型
  5. 在(表单' s?)加载事件中,填充新属性例如:

    chkugalis.Cost = 10.45
    chkugalis.ReceiptText = " plates of Ugali n fish "
  6. 将所有相关的MyCheckBoxes添加到collection的{​​{1}}

  7. 添加MyCheckBoxes块以构建收据。就像是:

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)

答案 1 :(得分:2)

查看您的代码,我可以看到它只会执行第一个已检查Integer.parseInt的代码。相反,你应该做的是分离你的CheckBox陈述:

If

这样,检查的每个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) 都会执行代码。