-
Jun 13th, 2021, 05:07 AM
#1
Thread Starter
New Member
[RESOLVED] How to do calculation using radio button and check box?
Here's the pic of my form
.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
-
Jun 13th, 2021, 06:48 AM
#2
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?
-
Jun 13th, 2021, 08:34 AM
#3
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.
-
Jun 13th, 2021, 08:50 AM
#4
Re: How to do calculation using radio button and check box?
Originally Posted by OptionBase1
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.
-
Jun 13th, 2021, 08:53 AM
#5
Re: How to do calculation using radio button and check box?
There's a simpler way to count the checked CheckBoxes:
vb.net Code:
Dim subjectCount = grpClassSize.Controls.OfType(Of CheckBox)().Count(Function(cb) cb.Checked)
-
Jun 13th, 2021, 10:18 AM
#6
Thread Starter
New Member
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
-
Jun 13th, 2021, 10:53 AM
#7
Re: How to do calculation using radio button and check box?
Originally Posted by jmcilhinney
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.
-
Jun 13th, 2021, 11:31 AM
#8
Re: How to do calculation using radio button and check box?
Originally Posted by OptionBase1
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.
-
Jun 13th, 2021, 07:39 PM
#9
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 14th, 2021, 08:56 AM
#10
Thread Starter
New Member
Re: How to do calculation using radio button and check box?
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
|