Probably dectxtBoxLabor is not a number.
To sum the labels, in VB you can try this: Val(lblLabor.Text) + Val(lblParts.Text) + Val(lblTax.Text), but there are more recommendations too. It seems easier to define and sum directly numeric variables.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Can someone tell me why I can not assign the same ("c") to my lblPart as I did my other labels? and also advise how to get the sum of all labels. Thanks.
Public Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim Sum1 As Double
Dim Result As Double
'Dim lblLabor As Double '<<< rename lblLabor
Dim CalcMiscChages As Double
Dim SumMisc As Double
Dim dblBoxLabor As Double
Dim dblBoxParts As Double
Dim dbllblLabor As Double
Dim dbllblParts As Double
Dim dbllblTax As Double
Dim decTotalCharges As Double
Dim decTax As Double
If ValidateInputFields() Then
Result = CalcOilLubeCharges(dblOilChange, dblLubeJob) + CalcMiscCharges(dblInspection, dblReplaceMuffler, dblTireRotation) + CalcFlushes(dblRadiatorFlush, dblTransmissionFlush)
lblLabor.Text = (txtBoxLabor.Text + Result).ToString("C")
lblParts.Text = dectxtBoxLabor
lblTax.Text = (txtBoxParts.Text * decTAX_RATE).ToString("c")
Probably dectxtBoxLabor is not a number.
To sum the labels, in VB you can try this: Val(lblLabor.Text) + Val(lblParts.Text) + Val(lblTax.Text), but there are more recommendations too. It seems easier to define and sum directly numeric variables.
You could use a custom label which has methods to set .Text as a specific format. Here we have one with several overloads with examples for two of them.
Imports System.ComponentModel
Public Class CurrencyFormattedLabel
Inherits Label
Public Sub New()
DoubleFormat = "c"
End Sub
Public Sub SetDoubleValue(value As Double)
Text = value.ToString(DoubleFormat)
End Sub
Public Sub SetDoubleValue(caption As String, value As Double)
Text = value.ToString(DoubleFormat)
Text = $"{caption} {value.ToString(DoubleFormat)}"
End Sub
Public Sub SetDoubleValue(value1 As Double, value2 As Double)
Text = (value1 + value2).ToString(DoubleFormat)
End Sub
Public Sub SetDoubleValue(caption As String, value1 As Double, value2 As Double)
Text = $"{caption} {(value1 + value2).ToString(DoubleFormat)}"
End Sub
Public Sub SetDoubleValue(caption As String, ParamArray args() As Double)
Text = $"{caption} {args.Sum().ToString(DoubleFormat)}"
End Sub
Public Sub SetDoubleValue(ParamArray args() As Double)
Text = args.Sum().ToString(DoubleFormat)
End Sub
<Category("Behavior"), Description("Format for SetDoubleValue")>
Public Property DoubleFormat As String
End Class
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CurrencyFormattedLabel1.SetDoubleValue("Total", 123.99, 23, 34.22)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
CurrencyFormattedLabel1.SetDoubleValue(123.99, 23.76, 34.22)
End Sub
End Class
In regards to calculate total charges, create a class such as below.
Perform assertions and calculations in PerformCalculation
, use CalculateTax
to calculate tax and if displaying in a label we use TotalLabel.Text = CalculateOrder.PresentTotal
.
Public Class CalculateOrder
Public Shared Property MiscellaneousCharges As Double
Public Shared Property MiscellaneousSum As Double
Public Shared Property Labor As Double
Public Shared Property Parts As Double
Public Shared Property Tax As Double
Public Shared ReadOnly Property Total As Double
Get
Return PerformCalculation()
End Get
End Property
Public Shared ReadOnly Property PresentTotal As String
Get
Return Total.ToString("C")
End Get
End Property
Private Shared Function PerformCalculation() As Double
Throw New NotImplementedException()
End Function
Private Shared Function CalculateTax() As Double
Throw New NotImplementedException()
End Function
End Class
Mockup usage
CalculateOrder.MiscellaneousCharges = 1
CalculateOrder.Labor = 1
CalculateOrder.Parts = 1
TotalLabel.Text = CalculateOrder.PresentTotal
Now we can augment the above by adding in an event if something goes wrong
Public Class CalculateOrder
Public Delegate Sub OnException(sender As String)
Public Shared Event OnExceptionEvent As OnException
Public Shared Property MiscellaneousCharges As Double
Public Shared Property MiscellaneousSum As Double
Public Shared Property Labor As Double
Public Shared Property Parts As Double
Public Shared Property Tax As Double
Public Shared ReadOnly Property Total As Double
Get
Return PerformCalculation()
End Get
End Property
Public Shared ReadOnly Property PresentTotal As String
Get
Return Total.ToString("C")
End Get
End Property
Private Shared Function PerformCalculation() As Double
Throw New NotImplementedException()
End Function
Private Shared Function CalculateTax() As Double
Throw New NotImplementedException()
End Function
End Class
In the form
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler CalculateOrder.OnExceptionEvent, AddressOf CalculationError
End Sub
Private Sub CalculationError(sender As String)
Throw New NotImplementedException()
End Sub
Notes
RaiseEvent OnExceptionEvent("Invalid amount")