Sylvie
Sylvie

Reputation: 3

How do I properly use checkboxes for running total in VB.net?

I'm a complete newbie when it comes to programming in VB.net (started last week). I have an assignment in class that requires me to do a running total using 3 checkboxes.

I've figured out a little bit of it. My main problem right now is that when I uncheck boxes, the calculation gets weird. So far here is my code:

Public Class frm1
    Dim HandbagAcc As Double = 0

    Private Sub chkBoxExtra_CheckedChanged(sender As Object, e As EventArgs) Handles chkBoxExtra.CheckedChanged
        '$9.99
        If chkBoxExtra.Checked = True Then
            HandbagAcc = HandbagAcc + 9.99
            txtBoxAcc.Text = FormatCurrency(HandbagAcc)
        End If
        If chkBoxExtra.Checked = False Then
            HandbagAcc = HandbagAcc - 9.99
            txtBoxAcc.Text = FormatCurrency(HandbagAcc)
        End If
    End Sub

    Private Sub chkBoxMatching_CheckedChanged(sender As Object, e As EventArgs) Handles chkBoxMatching.CheckedChanged
        '$7.99
        If chkBoxExtra.Checked = True Then
            HandbagAcc = HandbagAcc + 7.99
            txtBoxAcc.Text = FormatCurrency(HandbagAcc)
        End If

        If chkBoxExtra.Checked = False Then
            HandbagAcc = HandbagAcc - 7.99
            txtBoxAcc.Text = FormatCurrency(HandbagAcc)
        End If
    End Sub
    Private Sub chkBoxFour_CheckedChanged(sender As Object, e As EventArgs) Handles chkBoxFour.CheckedChanged
        '$3.95
        If chkBoxExtra.Checked = True Then
            HandbagAcc = HandbagAcc + 3.95
            txtBoxAcc.Text = FormatCurrency(HandbagAcc)
        End If

        If chkBoxExtra.Checked = False Then
            HandbagAcc = (HandbagAcc - 3.95)
            txtBoxAcc.Text = FormatCurrency(HandbagAcc)
        End If
    End Sub
End Class

When I check the three checkboxes the first time, the values add up nicely--but, when I uncheck a box or two, my numbers go wonky and the calculation doesn't add up. What am I doing wrong?

Any help would be much appreciated. Thank you!

Upvotes: 0

Views: 257

Answers (2)

Trevor_G
Trevor_G

Reputation: 1331

SImpler method, make use of a more flexible property

REMOVE

Dim HandbagAcc As Double = 0

REPLACE WITH

Public ReadOnly Property HandbagAcc() as Double
    Get
        Dim Sum As Double
        If chkBoxExtra.Checked Then Sum += 9.99
        If chkBoxMatching.checked Then Sum += 7.99
        If chkBoxFour.Checked Then Sum +=  3.95
        Return Sum
    End Get
End Sub

CREATE COMMON HANDLER

Private Sub AnyCheckBox_CheckChanged(sender As Object, e As EventArgs) _
       Handles _
       chkBoxExtra.CheckedChanged, _
       chkBoxMatching.CheckedChanged, _ 
       chkBoxFour.CheckedChanged

    txtBoxAcc.Text = FormatCurrency(HandbagAcc)

End Sub

Upvotes: 0

A Friend
A Friend

Reputation: 2750

So as commented, you were checking the wrong checkbox in two of the handlers.

I'd like to offer some improvement on the code anyway. If you add your prices to the checkboxes as a tag, you can dramatically reduce the amount of code needed.

Dim HandbagAcc As Double = 0

Private Sub AnyCheckBox_CheckChanged(sender As Object, e As EventArgs) Handles chkBoxExtra.CheckedChanged, chkBoxMatching.CheckedChanged, chkBoxFour.CheckedChanged
    Dim currentChk As CheckBox = sender
    Dim price As Double = currentChk.Tag
    HandbagAcc = HandbagAcc + If(currentChk.Checked, price, -price)
    txtBoxAcc.Text = FormatCurrency(HandbagAcc)
End Sub

So, having added the tags, you don't need a separate handler for each one, and can run it all through the same handler: Handles chkBoxExtra.CheckedChanged, chkBoxMatching.CheckedChanged, chkBoxFour.CheckedChanged

The specific checkbox is grabbed through the sender parameter and cast to CheckBox so you can access specific properties of that type.

Grab the tag and cast to Double so you have the value to add/subtract from your total.

Now this line:

HandbagAcc = HandbagAcc + If(currentChk.Checked, price, -price)

The inline If allows you to compact the following:

If currentChk.Checked Then
    HandbagAcc += price
Else
    HandbagAcc -= price
End If

+= being the short way of doing x = x + y

Upvotes: 2

Related Questions