|
-
Feb 23rd, 2008, 10:59 AM
#1
Thread Starter
Lively Member
[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?
-
Feb 23rd, 2008, 11:21 AM
#2
Re: On the fly math with checkboxes
this works with 4 textboxes + 4 checkboxes
(named textbox1, textbox2, checkbox1, checkbox2, etc)
vb Code:
Private Sub TextBoxes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged
doTotal()
End Sub
Private Sub CheckBoxes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged
doTotal()
End Sub
Private Sub doTotal()
Dim total As Decimal = 0
For x As Integer = 1 To 4
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
Label1.Text = CStr(total)
End Sub
you'd have to modify the handles statements in TextBoxes_TextChanged + CheckBoxes_CheckedChanged to include 14 textboxes + checkboxes
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 03:36 PM
#3
Thread Starter
Lively Member
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
-
Feb 23rd, 2008, 03:39 PM
#4
Re: On the fly math with checkboxes
are your checkboxes named checkbox1 ..to checkbox14?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 04:52 PM
#5
Thread Starter
Lively Member
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
-
Feb 23rd, 2008, 05:17 PM
#6
Hyperactive Member
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"--
-
Feb 23rd, 2008, 05:22 PM
#7
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:
Public Class Form1
Private textboxArray() As TextBox
Private checkboxArray() As CheckBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
textboxArray = New TextBox() {tbxSelfPt, tbxTattoosPt, tbxSymbolismPt, tbxdocsPt, tbxPubsPt, tbxAuthorshipPt, tbxCourtRecsPt, tbxPhotosPt, tbxAssociationPt, tbxContactsPt, tbxCInformationPt, tbxMembershipPt, tbxAgenciesPt, tbxMediaPt}
checkboxArray = New CheckBox() {cbxSelf, cbxTattoos, cbxSymbolism, cbxDocs, cbxPubs, cbxAuthorship, cbxCourtRecs, cbxPhotos, cbxAssociation, cbxContacts, cbxCInformation, cbxmembership, cbxAgencies, cbxMedia}
End Sub
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 checkboxArray(x - 1).Checked Then
total += CDec(Val(textboxArray(x - 1).Text))
Else
total -= CDec(Val(textboxArray(x - 1).Text))
End If
Next
tbxTotalPt.Text = CStr(total)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 05:25 PM
#8
Re: On the fly math with checkboxes
 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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 05:47 PM
#9
Hyperactive Member
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"--
-
Feb 23rd, 2008, 05:50 PM
#10
Hyperactive Member
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"--
-
Feb 23rd, 2008, 05:51 PM
#11
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 05:56 PM
#12
Re: On the fly math with checkboxes
actually it'd take more typing to use addhandlers than the way i did it
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 24th, 2008, 02:18 PM
#13
Thread Starter
Lively Member
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:
Private Sub cbxSelf_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxSelf.CheckedChanged
If cbxSelf.Checked = True Then
tbxSelfPt.Text = "5"
Else
tbxSelfPt.Text = ""
End If
Private Sub cbxTattoos_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxTattoos.CheckedChanged
If cbxTattoos.Checked = True Then
tbxTattoosPt.Text = "7"
Else
tbxTattoosPt.Text = ""
End If
-
Feb 24th, 2008, 06:17 PM
#14
Thread Starter
Lively Member
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:
Private textboxArray() As TextBox
Private checkboxArray() As CheckBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
textboxArray = New TextBox() {tbxSelfPt, tbxTattoosPt, tbxSymbolismPt, tbxdocsPt, tbxPubsPt, tbxAuthorshipPt, tbxCourtRecsPt, tbxPhotosPt, tbxAssociationPt, tbxContactsPt, tbxCInformationPt, tbxMembershipPt, tbxAgenciesPt, tbxMediaPt}
checkboxArray = New CheckBox() {cbxSelf, cbxTattoos, cbxSymbolism, cbxDocs, cbxPubs, cbxAuthorship, cbxCourtRecs, cbxPhotos, cbxAssociation, cbxContacts, cbxCInformation, cbxmembership, cbxAgencies, cbxMedia}
StatusBarPanel2.Text = Date.Today
End Sub
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
If cbxSelf.Checked = True Then
tbxSelfPt.Text = "5"
Else
tbxSelfPt.Text = ""
End If
If cbxTattoos.Checked = True Then
tbxTattoosPt.Text = "7"
Else
tbxTattoosPt.Text = ""
End If
If cbxSymbolism.Checked = True Then
tbxSymbolismPt.Text = "2"
Else
tbxSymbolismPt.Text = ""
End If
If cbxDocs.Checked = True Then
tbxdocsPt.Text = "5"
Else
tbxdocsPt.Text = ""
End If
If cbxPubs.Checked = True Then
tbxPubsPt.Text = "1"
Else
tbxPubsPt.Text = ""
End If
If cbxAuthorship.Checked = True Then
tbxAuthorshipPt.Text = "7"
Else
tbxAuthorshipPt.Text = ""
End If
If cbxCourtRecs.Checked = True Then
tbxCourtRecsPt.Text = "9"
Else
tbxCourtRecsPt.Text = ""
End If
If cbxPhotos.Checked = True Then
tbxPhotosPt.Text = "2"
Else
tbxPhotosPt.Text = ""
End If
If cbxAssociation.Checked = True Then
tbxAssociationPt.Text = "2"
Else
tbxAssociationPt.Text = ""
End If
If cbxContacts.Checked = True Then
tbxContactsPt.Text = "2"
Else
tbxContactsPt.Text = ""
End If
If cbxCInformation.Checked = True Then
tbxCInformationPt.Text = "2"
Else
tbxCInformationPt.Text = ""
End If
If cbxmembership.Checked = True Then
tbxMembershipPt.Text = "9"
Else
tbxMembershipPt.Text = ""
End If
If cbxAgencies.Checked = True Then
tbxAgenciesPt.Text = "8"
Else
tbxAgenciesPt.Text = ""
End If
If cbxMedia.Checked = True Then
tbxMediaPt.Text = "5"
Else
tbxMediaPt.Text = ""
End If
doTotal()
End Sub
Private Sub doTotal()
Dim total As Decimal = 0
For x As Integer = 1 To 14
If checkboxArray(x - 1).Checked Then
total += CDec(Val(textboxArray(x - 1).Text))
Else
total -= CDec(Val(textboxArray(x - 1).Text))
End If
Next
tbxTotalPt.Text = CStr(total)
End Sub
Private Sub StatusBar1_PanelClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.StatusBarPanelClickEventArgs) Handles StatusBar1.PanelClick
StatusBarPanel1.Text = Date.Now
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|