question

Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 asked karenpayneoregon edited

Label.Text to ("c") Help

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")
dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Hekzdaddy-3952 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That looks like it may work, I'm supposed to use the TryPaarse and calculate Total Charges procedure. Any thoughts?

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered Hekzdaddy-3952 commented

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.

82100-f1.png

Custom label

 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

Usage


 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



f1.png (6.9 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That looks like it may work, any suggestions om how to include TryParse and Calculate Total Charges procedure?

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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

  • Throw New NotImplementedException() represent work needs to be done e.g. assertions such as divide by zero, invalid values etc. and do the calculations

  • If you need to perform calculations on each part per-say you of course need to account for that.

  • How to use the event sample RaiseEvent OnExceptionEvent("Invalid amount")

  • When there are additional question not specific to the original question close out the first thread when satisfied then open a new thread/question.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.