Vb.net 处理多个复选框控件

Vb.net 处理多个复选框控件,vb.net,checkbox,Vb.net,Checkbox,我想对我正在创建的具有15个复选框控件的系统提供一些帮助。系统可同时检查所有这些复选框控件,或至少同时检查其中五个控件 我知道如何使用If[…]Else来处理它,但这很乏味,需要太多的编码 有没有人能告诉我有没有更好更简单的方法 下面是我的操作方法,甚至在开始对复选框控件进行多项选择之前,我有几行代码: Private Sub computeCurrentSelection() If chkugalis.Checked = True Then 'ugali fish selected

我想对我正在创建的具有15个
复选框
控件的系统提供一些帮助。系统可同时检查所有这些
复选框
控件,或至少同时检查其中五个控件

我知道如何使用
If[…]Else
来处理它,但这很乏味,需要太多的编码

有没有人能告诉我有没有更好更简单的方法

下面是我的操作方法,甚至在开始对
复选框
控件进行多项选择之前,我有几行代码:

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

查看您的代码,我可以看到它只会执行第一个选中的
复选框的代码。相反,您应该做的是将
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)

这样,每个选中的
复选框都会执行其代码。

查看您的代码,我可以看到它只会执行第一个选中的
复选框的代码。相反,您应该做的是将
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)

这样,选中的每个
复选框都将执行其代码。

如果您是初学者,并且您的解决方案按照您希望的方式工作,那么它没有问题。我能想到的其他解决方案可能被认为比您正在做的事情“更复杂”,但肯定会为您节省大量代码

我不知道为什么您的
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
不过,如果是我编写了一个解决方案来完成(看起来)您要做的事情,那么我会这样做:

  • 创建一个
    公共类MyCheckBox
    ,该类
    继承Checkbox
  • 添加两个新属性:
    Cost
    ReceiptText
  • 将当前的
    复选框
    控件更改为类型
    MyCheckBox
  • 在(表单的?)加载事件中,填充新属性,例如:

    chkugalis.Cost = 10.45 chkugalis.ReceiptText = " plates of Ugali n fish "
  • 如果您是初学者,并且您的解决方案按照您希望的方式工作,那么它没有问题。我能想到的其他解决方案可能被认为比您正在做的事情“更复杂”,但肯定会为您节省大量代码

    我不知道为什么您的
    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
    
    不过,如果是我编写了一个解决方案来完成(看起来)您要做的事情,那么我会这样做:

  • 创建一个
    公共类MyCheckBox
    ,该类
    继承Checkbox
  • 添加两个新属性:
    Cost
    ReceiptText
  • 将当前的
    复选框
    控件更改为类型
    MyCheckBox
  • 在(表单的?)加载事件中,填充新属性,例如:

    chkugalis.Cost = 10.45 chkugalis.ReceiptText = " plates of Ugali n fish "