Results 1 to 10 of 10

Thread: [RESOLVED] How to do calculation using radio button and check box?

  1. #1

    Thread Starter
    New Member HappyPenguin's Avatar
    Join Date
    Jun 2021
    Posts
    3

    Resolved [RESOLVED] How to do calculation using radio button and check box?

    Here's the pic of my form
    Name:  Screenshot 2021-06-13 175136.jpg
Views: 185
Size:  14.4 KB
    .For class size, small =$80, medium = $75, large = $70 and $35/per subject. I need to calculate the total. There're some problems with my code, can u guys give some suggestions please.
    Code:
      Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Dim dblTotal As Double
            Dim checkedCheckBox As Integer = 0 'CheckBox checked
            Dim totalCheckBox As Integer = 0 'Total CheckBox on Group Box
    
            For Each myControl As CheckBox In Me.grpClassSize.Controls.OfType(Of CheckBox)()
                If myControl.Checked Then
                    checkedCheckBox += 1
                End If
                totalCheckBox += 1
            Next
    
            If radSmall.Checked = True Then
                If totalCheckBox = 1 Then
                    dblTotal = 80 * (35 * 1)
                ElseIf totalCheckBox = 2 Then
                    dblTotal = 80 * (35 * 2)
                ElseIf totalCheckBox = 3 Then
                    dblTotal = 80 * (35 * 3)
                ElseIf totalCheckBox = 4 Then
                    dblTotal = 80 * (35 * 4)
                End If
            End If
    
            If radMedium.Checked = True Then
                If totalCheckBox = 1 Then
                    dblTotal = 75 * (35 * 1)
                ElseIf totalCheckBox = 2 Then
                    dblTotal = 75 * (35 * 2)
                ElseIf totalCheckBox = 3 Then
                    dblTotal = 75 * (35 * 3)
                ElseIf totalCheckBox = 4 Then
                    dblTotal = 75 * (35 * 4)
                End If
            End If
    
            If radLarge.Checked = True Then
                If totalCheckBox = 1 Then
                    dblTotal = 70 * (35 * 1)
                ElseIf totalCheckBox = 2 Then
                    dblTotal = 70 * (35 * 2)
                ElseIf totalCheckBox = 3 Then
                    dblTotal = 70 * (35 * 3)
                ElseIf totalCheckBox = 4 Then
                    dblTotal = 70 * (35 * 4)
                End If
            End If
    
            lblTotal.Text = FormatCurrency(dblTotal, 2)
        End Sub
    Last edited by si_the_geek; Jun 13th, 2021 at 06:27 AM. Reason: added Code tags

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,158

    Re: How to do calculation using radio button and check box?

    You need to debug your own code. If you don't know how to debug, learn now. Set a breakpoint at the top of your code and then step through it line by line. At each step consider what it is that you expect to happen. After the step, check what actually did happen and see whether they are the same. If not then you have found an issue and you can diagnose that issue specifically. If you need our help then you can provide us with all the information you found when you debugged.

    That said, there are some obvious issue with that code. Firstly, do you not see what's wrong with this?
    Code:
                If totalCheckBox = 1 Then
                    dblTotal = 75 * (35 * 1)
                ElseIf totalCheckBox = 2 Then
                    dblTotal = 75 * (35 * 2)
                ElseIf totalCheckBox = 3 Then
                    dblTotal = 75 * (35 * 3)
                ElseIf totalCheckBox = 4 Then
                    dblTotal = 75 * (35 * 4)
                End If
    Why do you have all that code to what you could do in one line? The value you use in the code is the same as totalCheckBox every time so why not just use totalCheckBox?

    Secondly, do you not see what's wrong with this:
    Code:
            If radSmall.Checked = True Then
                If totalCheckBox = 1 Then
                    dblTotal = 80 * (35 * 1)
                ElseIf totalCheckBox = 2 Then
                    dblTotal = 80 * (35 * 2)
                ElseIf totalCheckBox = 3 Then
                    dblTotal = 80 * (35 * 3)
                ElseIf totalCheckBox = 4 Then
                    dblTotal = 80 * (35 * 4)
                End If
            End If
    
            If radMedium.Checked = True Then
                If totalCheckBox = 1 Then
                    dblTotal = 75 * (35 * 1)
                ElseIf totalCheckBox = 2 Then
                    dblTotal = 75 * (35 * 2)
                ElseIf totalCheckBox = 3 Then
                    dblTotal = 75 * (35 * 3)
                ElseIf totalCheckBox = 4 Then
                    dblTotal = 75 * (35 * 4)
                End If
            End If
    Again, everything is the same in those two blocks except for one value so why repeat everything else when you could just assign that value to a variable and then use that variable in one line of code? You only need one line to do the calculation. You get a number of the RadioButton selection and a number for the CheckBox selections and you those numbers in a single calculation.

    Finally:
    Code:
            If radSmall.Checked = True Then
                If totalCheckBox = 1 Then
                    dblTotal = 80 * (35 * 1)
                ElseIf totalCheckBox = 2 Then
                    dblTotal = 80 * (35 * 2)
                ElseIf totalCheckBox = 3 Then
                    dblTotal = 80 * (35 * 3)
                ElseIf totalCheckBox = 4 Then
                    dblTotal = 80 * (35 * 4)
                End If
            End If
    
            If radMedium.Checked = True Then
                If totalCheckBox = 1 Then
                    dblTotal = 75 * (35 * 1)
                ElseIf totalCheckBox = 2 Then
                    dblTotal = 75 * (35 * 2)
                ElseIf totalCheckBox = 3 Then
                    dblTotal = 75 * (35 * 3)
                ElseIf totalCheckBox = 4 Then
                    dblTotal = 75 * (35 * 4)
                End If
            End If
    If you have already tested on RadioButton and found it to checked, you know for a fact that no other RadioButton can be checked, so what's the point of testing to see whether it is?

  3. #3
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,638

    Re: How to do calculation using radio button and check box?

    One other thing, presumably your total cost calculations should be based on how many checkboxes are checked. Your code right now seems to calculate and store how many checkboxes are checked but then does nothing with that variable.

    Good luck.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,158

    Re: How to do calculation using radio button and check box?

    Quote Originally Posted by OptionBase1 View Post
    Your code right now seems to calculate and store how many checkboxes are checked but then does nothing with that variable.
    It does use that value but it uses it poorly. It's used in the If statements instead of directly in the calulation.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,158

    Re: How to do calculation using radio button and check box?

    There's a simpler way to count the checked CheckBoxes:
    vb.net Code:
    1. Dim subjectCount = grpClassSize.Controls.OfType(Of CheckBox)().Count(Function(cb) cb.Checked)

  6. #6

    Thread Starter
    New Member HappyPenguin's Avatar
    Join Date
    Jun 2021
    Posts
    3

    Re: How to do calculation using radio button and check box?

    I've done some correction. Finally i can run it. Is there any changes I need to do to make it more perfect?

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim intClassSize As Integer
    Dim intCount As Integer
    Dim dblTotal As Double

    intCount = 0
    For Each c As Control In grpSubject.Controls
    If TypeOf c Is CheckBox Then
    If CType(c, CheckBox).Checked Then
    intCount += 1
    End If
    End If
    Next

    If radSmall.Checked = True Then
    intClassSize = 80
    ElseIf radMedium.Checked = True Then
    intClassSize = 75
    ElseIf radLarge.Checked = True Then
    intClassSize = 70
    End If

    dblTotal = intClassSize + (intCount * 35)
    lblTotal.Text = FormatCurrency(dblTotal, 2)

    End Sub

  7. #7
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,638

    Re: How to do calculation using radio button and check box?

    Quote Originally Posted by jmcilhinney View Post
    It does use that value but it uses it poorly. It's used in the If statements instead of directly in the calulation.
    Nope, it certainly doesn't. Look carefully.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,158

    Re: How to do calculation using radio button and check box?

    Quote Originally Posted by OptionBase1 View Post
    Nope, it certainly doesn't. Look carefully.
    I see what you mean. I missed the difference between checkedCheckBox and totalCheckBox. This code is yet another example of what happens when you write code without knowing what is has to do beforehand. If someone was to do this calculation by hand, it would not look much like the code posted here. That's why I always say that people should work out how they would do something manually, devise an algorithm based on that and then write code specifically to implement that algorithm.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,055

    Re: How to do calculation using radio button and check box?

    Try this...

    Code:
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click  
    
        Dim intCount As Integer = grpSubject.Controls.OfType(Of CheckBox).Count(Function(c) c.Checked)  
        Dim intClassSize As Integer = If(radSmall.Checked, 80, If(radMedium.Checked, 75, If(radLarge.Checked, 70, 0)))
    
        Dim decTotal As Decimal = CDec(intClassSize + (intCount * 35))
        lblTotal.Text = decTotal.ToString("c2")
    
    End Sub

  10. #10

    Thread Starter
    New Member HappyPenguin's Avatar
    Join Date
    Jun 2021
    Posts
    3

    Re: How to do calculation using radio button and check box?

    Thanks! Solved it.

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