Label.Text to ("c") Help

Hekzdaddy 121 Reputation points
2021-03-28T03:20:11.517+00:00

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")
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2021-03-28T10:03:28.88+00:00

    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.


  2. Karen Payne MVP 35,036 Reputation points
    2021-03-28T11:20:33.22+00:00

    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  
      
    

  3. Karen Payne MVP 35,036 Reputation points
    2021-03-28T16:37:58.553+00:00

    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.
    0 comments No comments