Results 1 to 14 of 14

Thread: [RESOLVED] On the fly math with checkboxes

  1. #1

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Resolved [RESOLVED] On the fly math with checkboxes

    I have 14 categories with checkboxes and textboxes for "points" for each category. I also have a texbox for the total "points" that add each category that is activated by the checkbox. I want the total points to add the 14 textboxes that are activated or subtract when not activated. I can't seem to get it to work "on the fly" on the form but works great with a button click event. The code was in the FROM LOAD event where I think my problem is but not sure, if so what event would I put the code under? Or do I need some alternate code? How can I do the math automatically with checkbox states. I can't see using if/then or case/select with 14 categories, would be an awful lot of code. Any ideas?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: On the fly math with checkboxes

    this works with 4 textboxes + 4 checkboxes
    (named textbox1, textbox2, checkbox1, checkbox2, etc)

    vb Code:
    1. Private Sub TextBoxes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    2.     Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged
    3.     doTotal()
    4. End Sub
    5.  
    6. Private Sub CheckBoxes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    7.     Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged
    8.     doTotal()
    9. End Sub
    10.  
    11. Private Sub doTotal()
    12.     Dim total As Decimal = 0
    13.     For x As Integer = 1 To 4
    14.         If DirectCast(Me.Controls("checkbox" & x), CheckBox).Checked Then
    15.             total += CDec(Val(DirectCast(Me.Controls("textbox" & x), TextBox).Text))
    16.         Else
    17.             total -= CDec(Val(DirectCast(Me.Controls("textbox" & x), TextBox).Text))
    18.         End If
    19.     Next
    20.     Label1.Text = CStr(total)
    21. End Sub

    you'd have to modify the handles statements in TextBoxes_TextChanged + CheckBoxes_CheckedChanged to include 14 textboxes + checkboxes

  3. #3

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Re: On the fly math with checkboxes

    I'm getting a null reference exception when I try to run the program with this line of code

    Code:
    If DirectCast(Me.Controls("checkbox" & x), CheckBox).Checked Then

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: On the fly math with checkboxes

    are your checkboxes named checkbox1 ..to checkbox14?

  5. #5

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Re: On the fly math with checkboxes

    no but here is the entire code
    Code:
    Private Sub textboxes_textchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles tbxSelfPt.TextChanged, tbxTattoosPt.TextChanged, tbxSymbolismPt.TextChanged, tbxdocsPt.TextChanged, tbxPubsPt.TextChanged, tbxAuthorshipPt.TextChanged, tbxCourtRecsPt.TextChanged, tbxPhotosPt.TextChanged, tbxAssociationPt.TextChanged, tbxContactsPt.TextChanged, tbxCInformationPt.TextChanged, tbxMembershipPt.TextChanged, tbxAgenciesPt.TextChanged, tbxMediaPt.TextChanged
            doTotal()
    
        End Sub
    
        Private Sub checkboxes_checkchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles cbxSelf.CheckedChanged, cbxTattoos.CheckedChanged, cbxSymbolism.CheckedChanged, cbxDocs.CheckedChanged, cbxPubs.CheckedChanged, cbxAuthorship.CheckedChanged, cbxCourtRecs.CheckedChanged, cbxPhotos.CheckedChanged, cbxAssociation.CheckedChanged, cbxContacts.CheckedChanged, cbxCInformation.CheckedChanged, cbxmembership.CheckedChanged, cbxAgencies.CheckedChanged, cbxMedia.CheckedChanged
            doTotal()
    
        End Sub
    
        Private Sub doTotal()
            Dim total As Decimal = 0
            For x As Integer = 1 To 14
                If DirectCast(Me.Controls("checkbox" & x), CheckBox).Checked Then
                    total += CDec(Val(DirectCast(Me.Controls("textbox" & x), TextBox).Text))
                Else
                    total -= CDec(Val(DirectCast(Me.Controls("textbox" & x), TextBox).Text))
                End If
            Next
            tbxTotalPt.Text = CStr(total)
    
        End Sub

  6. #6
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    In A House :)
    Posts
    291

    Re: On the fly math with checkboxes

    remove the "" from around checkbox it's taking it as a string literal, an look up addressof
    --"Tap Dancing On The Brittle Edge Of Sanity"--

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: On the fly math with checkboxes

    for the code to work they needed to be named checkbox1, checkbox2 etc.
    heres a different way:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private textboxArray() As TextBox
    4.     Private checkboxArray() As CheckBox
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         textboxArray = New TextBox() {tbxSelfPt, tbxTattoosPt, tbxSymbolismPt, tbxdocsPt, tbxPubsPt, tbxAuthorshipPt, tbxCourtRecsPt, tbxPhotosPt, tbxAssociationPt, tbxContactsPt, tbxCInformationPt, tbxMembershipPt, tbxAgenciesPt, tbxMediaPt}
    8.         checkboxArray = New CheckBox() {cbxSelf, cbxTattoos, cbxSymbolism, cbxDocs, cbxPubs, cbxAuthorship, cbxCourtRecs, cbxPhotos, cbxAssociation, cbxContacts, cbxCInformation, cbxmembership, cbxAgencies, cbxMedia}
    9.     End Sub
    10.  
    11.     Private Sub textboxes_textchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    12.     Handles tbxSelfPt.TextChanged, tbxTattoosPt.TextChanged, tbxSymbolismPt.TextChanged, tbxdocsPt.TextChanged, tbxPubsPt.TextChanged, tbxAuthorshipPt.TextChanged, tbxCourtRecsPt.TextChanged, tbxPhotosPt.TextChanged, tbxAssociationPt.TextChanged, tbxContactsPt.TextChanged, tbxCInformationPt.TextChanged, tbxMembershipPt.TextChanged, tbxAgenciesPt.TextChanged, tbxMediaPt.TextChanged
    13.         doTotal()
    14.  
    15.     End Sub
    16.  
    17.     Private Sub checkboxes_checkchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    18.     Handles cbxSelf.CheckedChanged, cbxTattoos.CheckedChanged, cbxSymbolism.CheckedChanged, cbxDocs.CheckedChanged, cbxPubs.CheckedChanged, cbxAuthorship.CheckedChanged, cbxCourtRecs.CheckedChanged, cbxPhotos.CheckedChanged, cbxAssociation.CheckedChanged, cbxContacts.CheckedChanged, cbxCInformation.CheckedChanged, cbxmembership.CheckedChanged, cbxAgencies.CheckedChanged, cbxMedia.CheckedChanged
    19.         doTotal()
    20.  
    21.     End Sub
    22.  
    23.     Private Sub doTotal()
    24.         Dim total As Decimal = 0
    25.         For x As Integer = 1 To 14
    26.             If checkboxArray(x - 1).Checked Then
    27.                 total += CDec(Val(textboxArray(x - 1).Text))
    28.             Else
    29.                 total -= CDec(Val(textboxArray(x - 1).Text))
    30.             End If
    31.         Next
    32.         tbxTotalPt.Text = CStr(total)
    33.  
    34.     End Sub
    35.  
    36. End Class

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Question Re: On the fly math with checkboxes

    Quote Originally Posted by DirtyHowi
    remove the "" from around checkbox it's taking it as a string literal, an look up addressof
    how would that work?

  9. #9
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    In A House :)
    Posts
    291

    Re: On the fly math with checkboxes

    define a changed event without the handles, then add addhandler text1.textchanged, me.addressof changed_event in your load routine, saves having to type all the handles out. I put it in it's own sub called in form_load.

    directcast(object,object type) no ""

    also try for each loop rather than a counter, cause if you ever add a text box or subtract one you dont' have to change the logic to add or subtract the totals.
    --"Tap Dancing On The Brittle Edge Of Sanity"--

  10. #10
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    In A House :)
    Posts
    291

    Re: On the fly math with checkboxes

    Code:
        Dim ctl As Control
                Dim pnl As Panel
                pnl = pnlLegalGuardian
    
                For Each ctl In pnl.Controls
                    If TypeOf ctl Is TextBox Then
                        ctl.Text = ""
                    ElseIf TypeOf ctl Is ComboBox Then
                        DirectCast(ctl, ComboBox).SelectedIndex = -1
                    ElseIf TypeOf ctl Is MaskedTextBox Then
                        ctl.Text = ""
                    ElseIf TypeOf ctl Is DateTimePicker Then
                        DirectCast(ctl, DateTimePicker).Value = Date.Today
                    End If
                Next
    i use it to clear a tab in a tab control that are on a panel
    --"Tap Dancing On The Brittle Edge Of Sanity"--

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: On the fly math with checkboxes

    directcast(object,object type) no "" wouldn't have worked. i was calling the controls by name. no point of using addhandlers now, the typings done.

    the textboxes + checkboxes in my second solution are contained in arrays.
    the for x as integer loops are necessary.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: On the fly math with checkboxes

    actually it'd take more typing to use addhandlers than the way i did it

  13. #13

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Re: On the fly math with checkboxes

    OK well that seems to work, no errors but had to delete some of my code for it to work. The tbxTotalPt.text textbox displays a zero(0) like it should when I run the program and check a checkbox, only problem is I had to delete the code that assigned the "point value" for each checkbox that displayed its point value in the textbox. So where do I assign these values, this is what I had b4.

    vb Code:
    1. Private Sub cbxSelf_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxSelf.CheckedChanged
    2.  
    3.     If cbxSelf.Checked = True Then
    4.         tbxSelfPt.Text = "5"
    5.     Else
    6.         tbxSelfPt.Text = ""
    7.  
    8.     End If
    9.  
    10. Private Sub cbxTattoos_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxTattoos.CheckedChanged
    11.  
    12.     If cbxTattoos.Checked = True Then
    13.         tbxTattoosPt.Text = "7"
    14.     Else
    15.         tbxTattoosPt.Text = ""
    16.  
    17.     End If

  14. #14

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Re: On the fly math with checkboxes

    OK never mind I figured it out, here is the complete code for anyone needing in the future.

    vb Code:
    1. Private textboxArray() As TextBox
    2.     Private checkboxArray() As CheckBox
    3.  
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         textboxArray = New TextBox() {tbxSelfPt, tbxTattoosPt, tbxSymbolismPt, tbxdocsPt, tbxPubsPt, tbxAuthorshipPt, tbxCourtRecsPt, tbxPhotosPt, tbxAssociationPt, tbxContactsPt, tbxCInformationPt, tbxMembershipPt, tbxAgenciesPt, tbxMediaPt}
    8.         checkboxArray = New CheckBox() {cbxSelf, cbxTattoos, cbxSymbolism, cbxDocs, cbxPubs, cbxAuthorship, cbxCourtRecs, cbxPhotos, cbxAssociation, cbxContacts, cbxCInformation, cbxmembership, cbxAgencies, cbxMedia}
    9.         StatusBarPanel2.Text = Date.Today
    10.  
    11.  
    12.     End Sub
    13.  
    14.     Private Sub textboxes_textchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    15.     Handles tbxSelfPt.TextChanged, tbxTattoosPt.TextChanged, tbxSymbolismPt.TextChanged, tbxdocsPt.TextChanged, tbxPubsPt.TextChanged, tbxAuthorshipPt.TextChanged, tbxCourtRecsPt.TextChanged, tbxPhotosPt.TextChanged, tbxAssociationPt.TextChanged, tbxContactsPt.TextChanged, tbxCInformationPt.TextChanged, tbxMembershipPt.TextChanged, tbxAgenciesPt.TextChanged, tbxMediaPt.TextChanged
    16.        
    17.         doTotal()
    18.  
    19.     End Sub
    20.  
    21.     Private Sub checkboxes_checkchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    22.      Handles cbxSelf.CheckedChanged, cbxTattoos.CheckedChanged, cbxSymbolism.CheckedChanged, cbxDocs.CheckedChanged, cbxPubs.CheckedChanged, cbxAuthorship.CheckedChanged, cbxCourtRecs.CheckedChanged, cbxPhotos.CheckedChanged, cbxAssociation.CheckedChanged, cbxContacts.CheckedChanged, cbxCInformation.CheckedChanged, cbxmembership.CheckedChanged, cbxAgencies.CheckedChanged, cbxMedia.CheckedChanged
    23.  
    24.         If cbxSelf.Checked = True Then
    25.             tbxSelfPt.Text = "5"
    26.         Else
    27.             tbxSelfPt.Text = ""
    28.         End If
    29.  
    30.         If cbxTattoos.Checked = True Then
    31.             tbxTattoosPt.Text = "7"
    32.         Else
    33.             tbxTattoosPt.Text = ""
    34.         End If
    35.  
    36.         If cbxSymbolism.Checked = True Then
    37.             tbxSymbolismPt.Text = "2"
    38.         Else
    39.             tbxSymbolismPt.Text = ""
    40.         End If
    41.  
    42.         If cbxDocs.Checked = True Then
    43.             tbxdocsPt.Text = "5"
    44.         Else
    45.             tbxdocsPt.Text = ""
    46.         End If
    47.  
    48.         If cbxPubs.Checked = True Then
    49.             tbxPubsPt.Text = "1"
    50.         Else
    51.             tbxPubsPt.Text = ""
    52.         End If
    53.  
    54.         If cbxAuthorship.Checked = True Then
    55.             tbxAuthorshipPt.Text = "7"
    56.         Else
    57.             tbxAuthorshipPt.Text = ""
    58.         End If
    59.  
    60.         If cbxCourtRecs.Checked = True Then
    61.             tbxCourtRecsPt.Text = "9"
    62.         Else
    63.             tbxCourtRecsPt.Text = ""
    64.         End If
    65.  
    66.         If cbxPhotos.Checked = True Then
    67.             tbxPhotosPt.Text = "2"
    68.         Else
    69.             tbxPhotosPt.Text = ""
    70.         End If
    71.  
    72.         If cbxAssociation.Checked = True Then
    73.             tbxAssociationPt.Text = "2"
    74.         Else
    75.             tbxAssociationPt.Text = ""
    76.         End If
    77.  
    78.         If cbxContacts.Checked = True Then
    79.             tbxContactsPt.Text = "2"
    80.         Else
    81.             tbxContactsPt.Text = ""
    82.         End If
    83.  
    84.         If cbxCInformation.Checked = True Then
    85.             tbxCInformationPt.Text = "2"
    86.         Else
    87.             tbxCInformationPt.Text = ""
    88.         End If
    89.  
    90.         If cbxmembership.Checked = True Then
    91.             tbxMembershipPt.Text = "9"
    92.         Else
    93.             tbxMembershipPt.Text = ""
    94.         End If
    95.  
    96.         If cbxAgencies.Checked = True Then
    97.             tbxAgenciesPt.Text = "8"
    98.         Else
    99.             tbxAgenciesPt.Text = ""
    100.         End If
    101.  
    102.         If cbxMedia.Checked = True Then
    103.             tbxMediaPt.Text = "5"
    104.         Else
    105.             tbxMediaPt.Text = ""
    106.         End If
    107.  
    108.         doTotal()
    109.  
    110.     End Sub
    111.  
    112.     Private Sub doTotal()
    113.  
    114.         Dim total As Decimal = 0
    115.         For x As Integer = 1 To 14
    116.             If checkboxArray(x - 1).Checked Then
    117.                 total += CDec(Val(textboxArray(x - 1).Text))
    118.             Else
    119.                 total -= CDec(Val(textboxArray(x - 1).Text))
    120.             End If
    121.         Next
    122.         tbxTotalPt.Text = CStr(total)
    123.  
    124.     End Sub
    125.  
    126.     Private Sub StatusBar1_PanelClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.StatusBarPanelClickEventArgs) Handles StatusBar1.PanelClick
    127.         StatusBarPanel1.Text = Date.Now
    128.  
    129.  
    130.     End Sub
    131.  
    132.  
    133.  
    134. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width