从vb.net中的复选框列表计算子总计

时间:2016-10-15 06:54:59

标签: vb.net

您好我已经编写了以下代码来处理表单中复选框的选择。总计应根据时间增加然后显示。有人请帮助我如何与此有关。以下是我处理选择并尝试计算总计的方法。

    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 " & " Kshs" & total)
    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 " & " Kshs" & total)
    End If
    If chkUgaliB.Checked = True Then 'githeri selected
        orderAmt = lab2.Text
        total = githeri * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " plates of " & "Githeri " & " Kshs" & total)
    End If
    If chkPilau.Checked = True Then
        orderAmt = lab4.Text
        total = chapo * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " plates of " & "Pilau " & " Kshs" & total)
    End If
    If chkPizza.Checked = True Then
        orderAmt = lab5.Text
        total = pilau * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " plates of " & "Pizza " & " Kshs" & total)
    End If
    If chkMandazi.Checked = True Then
        orderAmt = lab6.Text
        total = pizza * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & "mandazi " & " Kshs" & total)
    End If
    If chkSamosa.Checked = True Then
        orderAmt = lab7.Text
        total = mandazi * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & "Samosa " & " Kshs" & total)
    End If
    If chkChapon.Checked = True Then
        orderAmt = lab8.Text
        total = samosa * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & "Chapati " & " Kshs" & total)
    End If
    If chkWater.Checked = True And chk300ml.Checked = True Then
        orderAmt = lab9.Text
        total = water1 * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " Bottles of 300ml Water " & " Kshs" & total)
    End If
    If chkWater.Checked = True And chk500ml.Checked = True Then
        orderAmt = lab9.Text
        total = water2 * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " Bottles of 500ml Water " & " Kshs" & 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 " & " Kshs" & total)
    End If
    If chkWater.Checked = True And chk2l.Checked = True Then
        orderAmt = lab9.Text
        total = water4 * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " Bottles of 2l Water " & " Kshs" & total)
    End If
    If chkSoda.Checked = True And chk300ml.Checked = True Then
        orderAmt = lab10.Text
        total = soda1 * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " Bottles of 300ml Soda " & " Kshs" & total)
    End If
    If chkSoda.Checked = True And chk500ml.Checked = True Then
        orderAmt = lab10.Text
        total = soda2 * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " Bottles of 500ml Soda " & " Kshs" & total)
    End If
    If chkSoda.Checked = True And chk1l.Checked = True Then
        orderAmt = lab10.Text
        total = soda3 * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " Bottles of 1l Soda " & " Kshs" & total)
    End If
    If chkSoda.Checked = True And chk2l.Checked = True Then
        orderAmt = lab10.Text
        total = soda4 * orderAmt
        lstReceipt.Items.Add(orderAmt & " Bottles of 2l Soda " & " Kshs" & total)
    End If
    If chkJuice.Checked = True And chk300ml.Checked = True Then
        orderAmt = lab11.Text
        total = juice1 * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " Bottles of 300ml juice " & " Kshs" & total)
    End If
    If chkJuice.Checked = True And chk500ml.Checked = True Then
        orderAmt = lab11.Text
        total = juice2 * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " Bottles of 500ml juice " & " Kshs" & total)
    End If
    If chkJuice.Checked = True And chk1l.Checked = True Then
        orderAmt = lab11.Text
        total = juice3 * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " Bottles of 1l juice " & " Kshs" & total)
    End If
    If chkJuice.Checked = True And chk2l.Checked = True Then
        orderAmt = lab11.Text
        total = juice4 * orderAmt
        subtotal = total
        lstReceipt.Items.Add(orderAmt & " Bottles of 2l juice " & " Kshs" & total)
    End If
End Sub

2 个答案:

答案 0 :(得分:2)

我不确定我是否理解正确,但我会试一试。

试试这个:

Private Function ComputeCurrentSelection() As Integer ' not sure for data type, you decide
    Dim total As Integer
    If chkugalis.Checked = True Then 'ugali fish selected
        orderAmt = lab.Text
        total += ugalif * orderAmt        
        lstReceipt.Items.Add(orderAmt & " plates of" & " Ugali n fish " & " Kshs" & total)
    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 " & " Kshs" & total)
    End If
    If chkUgaliB.Checked = True Then 'githeri selected
        orderAmt = lab2.Text
        total += githeri * orderAmt
        lstReceipt.Items.Add(orderAmt & " plates of " & "Githeri " & " Kshs" & total)
    End If ...

    Return total
End Function

答案 1 :(得分:1)

我试过这个

      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 " & " Kshs" & total)
    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 " & " Kshs" & total)
    End If
    If chkUgaliB.Checked = True Then 'githeri selected
        orderAmt = lab2.Text
        total = githeri * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " plates of " & "Githeri " & " Kshs" & total)
    End If
    If chkPilau.Checked = True Then
        orderAmt = lab4.Text
        total = chapo * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " plates of " & "Pilau " & " Kshs" & total)
    End If
    If chkPizza.Checked = True Then
        orderAmt = lab5.Text
        total = pilau * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " plates of " & "Pizza " & " Kshs" & total)
    End If
    If chkMandazi.Checked = True Then
        orderAmt = lab6.Text
        total = pizza * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & "mandazi " & " Kshs" & total)
    End If
    If chkSamosa.Checked = True Then
        orderAmt = lab7.Text
        total = mandazi * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & "Samosa " & " Kshs" & total)
    End If
    If chkChapon.Checked = True Then
        orderAmt = lab8.Text
        total = samosa * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & "Chapati " & " Kshs" & total)
    End If
    If chkWater.Checked = True And chk300ml.Checked = True Then
        orderAmt = lab9.Text
        total = water1 * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " Bottles of 300ml Water " & " Kshs" & total)
    End If
    If chkWater.Checked = True And chk500ml.Checked = True Then
        orderAmt = lab9.Text
        total = water2 * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " Bottles of 500ml Water " & " Kshs" & 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 " & " Kshs" & total)
    End If
    If chkWater.Checked = True And chk2l.Checked = True Then
        orderAmt = lab9.Text
        total = water4 * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " Bottles of 2l Water " & " Kshs" & total)
    End If
    If chkSoda.Checked = True And chk300ml.Checked = True Then
        orderAmt = lab10.Text
        total = soda1 * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " Bottles of 300ml Soda " & " Kshs" & total)
    End If
    If chkSoda.Checked = True And chk500ml.Checked = True Then
        orderAmt = lab10.Text
        total = soda2 * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " Bottles of 500ml Soda " & " Kshs" & total)
    End If
    If chkSoda.Checked = True And chk1l.Checked = True Then
        orderAmt = lab10.Text
        total = soda3 * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " Bottles of 1l Soda " & " Kshs" & total)
    End If
    If chkSoda.Checked = True And chk2l.Checked = True Then
        orderAmt = lab10.Text
        total += soda4 * orderAmt
        lstReceipt.Items.Add(orderAmt & " Bottles of 2l Soda " & " Kshs" & total)
    End If
    If chkJuice.Checked = True And chk300ml.Checked = True Then
        orderAmt = lab11.Text
        total = juice1 * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " Bottles of 300ml juice " & " Kshs" & total)
    End If
    If chkJuice.Checked = True And chk500ml.Checked = True Then
        orderAmt = lab11.Text
        total = juice2 * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " Bottles of 500ml juice " & " Kshs" & total)
    End If
    If chkJuice.Checked = True And chk1l.Checked = True Then
        orderAmt = lab11.Text
        total = juice3 * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " Bottles of 1l juice " & " Kshs" & total)
    End If
    If chkJuice.Checked = True And chk2l.Checked = True Then
        orderAmt = lab11.Text
        total = juice4 * orderAmt
        subtotal += total
        lstReceipt.Items.Add(orderAmt & " Bottles of 2l juice " & " Kshs" & total)
    End If
End Sub

并且它已经工作但是它计算并存储小计,因此在我调用computeCurrentSelection()的任何地方更新它,例如,如果在第一次调用时小计为10,请尝试清除之前的总数并像这样更新

    Private Sub updateTotal()
    lstTotal.Items.Clear()
    lstTotal.Items.Add("Sub Total =" & subtotal)
End Sub     

清除不起作用,而是继续将子总计的当前值递增前一个,直到我刷新程序。 我怎么解决这个问题?