0

I'm using VB.net and I'm having trouble trying to add different values when I select checkboxes and diplaying the total in a textbox. When unchecking them, it should be subtracting the values. Here is my code

"totals" is my textbox

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`
2
  • 2
    In all of your event, you check for "chstk.Checked = False". Is this what you want? is chstk the right variable to use? Commented Aug 26, 2014 at 14:34
  • @the_lotus, good catch, i didn't saw this :-) another reason to use a single method instead of copy/pasting and adapting Commented Aug 26, 2014 at 14:35

1 Answer 1

1

you only have to change all your Double.Parse(total) to total.ToString

you want to convert a number to string, not a number to a number.

also do not use double if you can, you will be hit by floating conversion issue

use decimal and you can also create a method to reduce the repetition of code

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
Sign up to request clarification or add additional context in comments.

6 Comments

it adds now, but the unchecking part is broken. when i unchecked all, the value of total becomes negative. and sometimes when i unchecked it, it doesn't subtract.
@DOODpls, what are the original state, all unchecked? some of them checked? all checked?
@DOODpls, found the issue, do not use double if you can. use decimal instead
it is unchecked, then i checked it, and it adds, no problem, but when i unchecked it, some of the checkbox doesnt subtract its values, and some subtracts it and making the "total" to negative or sometime it doesnt subtact all the checkbox that i unchecked
@DOODpls, in my testing i just created a new winform application, dropped 5 checkbox and a textbox on the form. if you have something different, update your question with more details
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.