VISUAL BASIC I have two checkboxes. Using the below code, if I check the first c
ID: 3845280 • Letter: V
Question
VISUAL BASIC
I have two checkboxes. Using the below code, if I check the first checkbox (cboOilChange), the total accuratly displays. If I check the second checkbox (cboLubeJob), the total accurately displays.
However, if I check both checkboxes, the total only calculates the amount of the first checkbox. How do I get this to add both totals together if both checkboxes are checked?
THANK YOU.
Public Function OilLubeCharges() As Double
Dim total As Double = 0
If cboOilChange.Checked = True Then
total += OilChange
ElseIf cboLubeJob.Checked Then
total += LubeJob
'ElseIf cboOilChange.Checked = True And cboLubeJob.Checked = True Then ****this is not working
' total += (OilChange + LubeJob) ****this is not working
End If
Return total
End Function
---------------------------------------------------------------------------------------
I'm not sure if it matters, but here is the entire program code:
Public Class frmTGAutomotive
Const OilChange As Double = 26
Const LubeJob As Double = 18
Const RadiatorFlush As Double = 30
Const TransmissionFlush As Decimal = 80
Const Inspection As Decimal = 15
Const MufflerReplacement As Decimal = 100
Const TireRotation As Decimal = 20
Public Function OilLubeCharges() As Double
Dim total As Double = 0
If cboOilChange.Checked = True Then
total += OilChange
ElseIf cboLubeJob.Checked Then
total += LubeJob
'ElseIf cboOilChange.Checked = True And cboLubeJob.Checked = True Then
' total += (OilChange + LubeJob)
End If
Return total
End Function
Public Function FlushCharges() As Double
Dim total As Double = 0
If cboRadiatorFlush.Checked = True Then
total += RadiatorFlush
ElseIf cboTransmissionFlush.Checked Then
total += TransmissionFlush
End If
Return total
End Function
Public Function MiscCharges() As Double
Dim total As Double = 0
If cboInspection.Checked = True Then
total += Inspection
ElseIf cboReplaceMuffler.Checked Then
total += MufflerReplacement
ElseIf cboTireRotation.Checked Then
total += TireRotation
End If
Return total
End Function
Public Function OtherCharges(ByVal parts As Double, ByVal labor As Double) As Double
'Dim OtherCharges As Double = parts + labor
Return OtherCharges
End Function
Public Function TaxCharges(ByVal partsValue As Double) As Double
Dim saleTax As Double
saleTax = partsValue * 0.06
Return saleTax
End Function
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
lblPartsCharges.Text = String.Empty
lblServiceAndLaborCharges.Text = String.Empty
lblTaxCharges.Text = String.Empty
lblTotalFeesCharges.Text = String.Empty
Dim Service_Labor As Double = 0
Dim salesTax As Double = 0
Dim Charges As Double = 0
Dim parts As Double = 0
Dim labor As Double = 0
Dim total As Double
'parts = CDec(txtParts.Text)
'labor = CInt(txtLabor.Text)
Double.TryParse(txtParts.Text.ToString(), parts)
Double.TryParse(txtLabor.Text.ToString(), labor)
If parts >= 0 Then
salesTax = TaxCharges(parts)
Else
parts = 0
salesTax = 0
MessageBox.Show("Parts value cannot be negative", "Warning")
End If
If labor >= 0 Then
Charges = OtherCharges(parts, labor)
Else
labor = 0.0
Charges = OtherCharges(parts, labor)
MessageBox.Show("Labor value cannot be negative", "Warning")
End If
'Call appropriate Functions and add the return values of functions and store it to variable Service_Labor
Service_Labor = OilLubeCharges() + FlushCharges() + MiscCharges() + Charges
lblServiceAndLaborCharges.Text = Service_Labor.ToString("C")
lblPartsCharges.Text = parts.ToString("C")
lblTaxCharges.Text = salesTax.ToString("C")
total = Service_Labor + parts + salesTax
lblTotalFeesCharges.Text = total.ToString("C")
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
cboOilChange.CheckState = 0
cboLubeJob.CheckState = 0
cboRadiatorFlush.CheckState = 0
cboTransmissionFlush.CheckState = 0
cboInspection.CheckState = 0
cboReplaceMuffler.CheckState = 0
cboTireRotation.CheckState = 0
txtParts.Clear()
txtLabor.Clear()
lblServiceAndLaborCharges.Text = ""
lblPartsCharges.Text = ""
lblTaxCharges.Text = ""
lblTotalFeesCharges.Text = ""
End Sub
End Class
Explanation / Answer
CORRECTED FUNCTION
Public Function OilLubeCharges() As Double
Dim total As Double = 0
If cboOilChange.Checked = True Then
total += OilChange
End If
If cboLubeJob.Checked = True Then
total += LubeJob
End If
Return total
End Function
EXPLANATION: You were using an if-else-if statement. In that case, the program just checks for a single condition which when satisfied moves out of the if-else-if loop. So when both the checkboxes were checked, the program was just looking at the first checkbox which was checked(which comes first in the condition) i.e.,
If cboOilChange.Checked = True Then
total += OilChange
and rest line of code(else-if) was skipped hence you were getting only the first total.
So to overcome this error you need to put the each condition in an individual if statement. So the program will check each if statement individually, hence giving the desired result.
If you have any doubts then let me know in the comments below. And if you liked it then please give a thumbs up.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.