Visual Basic 2010 - 具有设定价格的多个复选框计算

时间:2016-05-05 22:17:11

标签: vb.net

所以我正在尝试编程你已经有200美元的东西,在一个groupbox中我有3个不同的选项作为购买东西的复选框,还有一个groupbox有radibuttons,你只能选择3个选项中的1个。所有项目都有不同的费用......

我已经做了什么: 我已编码

 Dim money As Integer = 200

 Private Sub chkBoots_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBoots.CheckedChanged
        lblAmountOfMoney.Text = Val(money) - 20
    End Sub

Private Sub chkJacket_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkJacket.CheckedChanged
        lblAmountOfMoney.Text = Val(money) - 40
    End Sub

Private Sub chkUmbrella_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkUmbrella.CheckedChanged
        lblAmountOfMoney.Text = Val(money) - 10
    End Sub

  Private Sub rdbCreditCard_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbCreditCard.CheckedChanged
        lblAmountOfMoney.Text = Val(money) - 20
    End Sub

Private Sub rdbDebitCard_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbDebitCard.CheckedChanged
        lblAmountOfMoney.Text = Val(money) - 20
    End Sub

问题在于,每当选择多个复选框和单选按钮时,它只会从200美元中减去每个复选框或单选按钮的分配成本,而不是减去从200美元中选择的每个复选框和单选按钮费用...(例如,如果您按Jacket and Boots它只会减去其中一项费用,而不是从$ 200减去它们两个

非常感谢所有人的帮助!!!!

2 个答案:

答案 0 :(得分:0)

现在您只是更新Subs中的标签文本。您还需要更新货币变量。

添加

 money = money - price 

在所有Subs中,无论价格恰好在每个Sub中。

答案 1 :(得分:0)

你在正确显示价格方面做得很好,但忘了做真正的计算。

这可能是你想要的:

Dim money As Integer = 200

Private Sub chkBoots_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBoots.CheckedChanged
    lblAmountOfMoney.Text = Val(money) - 20
    money -= 20
End Sub

Private Sub chkJacket_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkJacket.CheckedChanged
    lblAmountOfMoney.Text = Val(money) - 40
    money -= 40
End Sub

Private Sub chkUmbrella_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkUmbrella.CheckedChanged
    lblAmountOfMoney.Text = Val(money) - 10
    money -= 10
End Sub

Private Sub rdbCreditCard_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbCreditCard.CheckedChanged
    lblAmountOfMoney.Text = Val(money) - 20
    money -= 20
End Sub

Private Sub rdbDebitCard_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbDebitCard.CheckedChanged
    lblAmountOfMoney.Text = Val(money) - 20
    money -= 20
End Sub