如何添加/减去多个复选框值

时间:2014-08-26 13:56:45

标签: vb.net checkbox

我使用的是VB.net,当我选中复选框并在文本框中拖拽总数时,我无法尝试添加不同的值。取消选中它们时,应该减去这些值。这是我的代码

"总计"是我的文本框

Dim total As Double

Private Sub cchstk_CheckStateChanged(sender As Object, e As EventArgs) Handles cchstk.CheckStateChanged
    If (cchstk.Checked = True) Then
        total = total + 109.99
        totals.Text = Double.Parse(total)
    ElseIf (chstk.Checked = False) Then
        total = total - 109.99
        totals.Text = Double.Parse(total)
    End If
End Sub

Private Sub cms_CheckStateChanged(sender As Object, e As EventArgs) Handles cms.CheckStateChanged
    If (cms.Checked = True) Then
        total = total + 79.99
        totals.Text = Double.Parse(total)
    ElseIf (chstk.Checked = False) Then
        total = total - 79.99
        totals.Text = Double.Parse(total)
    End If
End Sub

Private Sub prrdg_CheckStateChanged(sender As Object, e As EventArgs) Handles prrdg.CheckStateChanged
    If (prrdg.Checked = True) Then
        total = total + 49.99
        totals.Text = Double.Parse(total)
    ElseIf (chstk.Checked = False) Then
        total = total - 49.99
        totals.Text = Double.Parse(total)
    End If

End Sub

Private Sub gb_CheckStateChanged(sender As Object, e As EventArgs) Handles gb.CheckStateChanged
    If (gb.Checked = True) Then
        total = total + 29.99
        totals.Text = Double.Parse(total)
    ElseIf (chstk.Checked = False) Then
        total = total - 29.99
        totals.Text = Double.Parse(total)
    End If
End Sub

Private Sub nd_CheckStateChanged(sender As Object, e As EventArgs) Handles nd.CheckStateChanged
    If (nd.Checked = True) Then
        total = total + 29.99
        totals.Text = Double.Parse(total)
    ElseIf (chstk.Checked = False) Then
        total = total - 29.99
        totals.Text = Double.Parse(total)
    End If
End Sub`

1 个答案:

答案 0 :(得分:1)

您只需将所有Double.Parse(total)更改为total.ToString

即可

您希望将number转换为string,而不是number转换为number

如果可以的话,也不要使用double,你会遇到浮动转化问题

使用decimal,您还可以创建一种减少代码重复的方法

Dim total As Decimal

Private Sub ChangeValue(add As Boolean, value As Decimal)
    total += If(add, value, -value)
    TextBox1.Text = total.ToString
End Sub

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    ChangeValue(DirectCast(sender, CheckBox).Checked, 109.99D)
End Sub

Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
    ChangeValue(DirectCast(sender, CheckBox).Checked, 79.99D)
End Sub

Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.CheckedChanged
    ChangeValue(DirectCast(sender, CheckBox).Checked, 49.99D)
End Sub

Private Sub CheckBox4_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox4.CheckedChanged
    ChangeValue(DirectCast(sender, CheckBox).Checked, 29.99D)
End Sub

Private Sub CheckBox5_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox5.CheckedChanged
    ChangeValue(DirectCast(sender, CheckBox).Checked, 29.99D)
End Sub