在使用复选框VB更改标签后,如何将标签的值重置为上一个值

时间:2014-12-07 16:57:33

标签: vb.net checkbox

我正在尝试在检查chkInsurence时为每位乘客增加10%的保险,并且当未经检查时将其返回到相同的值。这是我的代码

Private Sub chkInsurance_CheckedChanged(sender As Object, e As EventArgs) Handles chkInsurance.CheckedChanged
    If chkInsurance.Checked Then
        price += CDec(price * 0.1) * nudPassengers.Value
    Else
        price -= CDec(price * 0.1) * nudPassengers.Value
    End If
    lblPrice.Text = price.ToString("C")
End Sub

当Else运行时,它需要当前值的10%并减去而不是原始值的10%。

2 个答案:

答案 0 :(得分:0)

您可以将价格标签的值存储在变量中,当用户取消选中时,将存储的值分配给标签

答案 1 :(得分:0)

我修改了另一个变量而不是Idle_Mind建议的价格

Private Sub chkInsurance_CheckedChanged(sender as Object,e As EventArgs)Handles chkInsurance.CheckedChanged

    Dim insurenceTotal As Decimal
    If chkInsurance.Checked Then
        insurenceTotal = CDec(price + (price * 0.1) * nudPassengers.Value)
        lblPrice.Text = insurenceTotal.ToString("C")
    Else
        lblPrice.Text = price.ToString("C")
    End If
End Sub