【问题标题】:Processing multiple CheckBox controls处理多个 CheckBox 控件
【发布时间】:2017-05-18 17:29:50
【问题描述】:

对于我正在创建的具有 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

【问题讨论】:

    标签: vb.net checkbox


    【解决方案1】:

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

    我不确定为什么您的 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. 在(表单的?)加载事件中,填充新属性,例如:

      chkugalis.Cost = 10.45
      chkugalis.ReceiptText = "Ugalin 鱼盘"
    5. 将所有相关的MyCheckBoxes 添加到collectionMyCheckBoxes

    6. 添加一个For Each mchkb In MyCheckBoxesCollection 块来构建您的收据。就像是:

    对于每个 mchkb 作为 MyCheckBox IN MyCheckBoxesCollection
        如果 mchkb.Checked = True 那么
            orderAmt = lab.Text
            小计 = mchkb.Cost * orderAmt
            总计 = 总计 + 小计
            lstReceipt.Items.Add(orderAmt & mchkb.ReceiptText & subTotal.ToString)
        万一
    下一个
    lstReceipt.Items.Add("Total: " & total.ToString)

    【讨论】:

      【解决方案2】:

      查看您的代码我可以看到它只会执行第一个检查的代码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)
      

      这样每个被检查的CheckBox 都会执行它的代码。

      【讨论】:

        猜你喜欢
        • 2011-11-25
        • 1970-01-01
        • 1970-01-01
        • 2017-03-11
        • 1970-01-01
        • 2021-05-22
        • 1970-01-01
        • 2014-11-29
        • 1970-01-01
        相关资源
        最近更新 更多