Adding a value to a Label that already has a value (Numeric / Currency)

Gary Simpson 471 Reputation points
2022-03-08T15:26:42.927+00:00

Hi Great People

I am having a issue with adding a value to a value in a Label.

Basically When I select Data from my Database. My Labels are filled with Currency Values. ( LbSubTotal ), ( LbVAT ), ( LbNet )

I also have two textboxes ( TxtWaitingTime ) , ( TxtTolls )

Is what I am trying to achieve is, when I need to enter a value into One or Both of the textboxes, I then want to add the value of the textbox(s) to a Label ( LbNet).

This means I want to add the value of the two textboxes (if any value(s) have been inserted). to the label LbNet.

I have tried Code that was written for me by a great person ( LesHay ) I have tried manipulating LesHay's Code to suit the issue I have. to no Avail.
The Code I have...

Private Sub TxtWaitingTime_TextChanged(sender As Object, e As EventArgs) Handles TxtWaitingTime.TextChanged, TxtTolls.TextChanged  
  
        Dim tb As TextBox = DirectCast(sender, TextBox)  
        If Not tb.Name = "TxtWaitingTime" AndAlso Not tb.Name = "TxtTolls" AndAlso Not tb.Text.StartsWith("") Then  
            tb.Text = "£" & tb.Text  
            tb.SelectionLength = 0  
            tb.SelectionStart = tb.Text.Length  
  
        End If  
        SetTB()  
    End Sub  
  
    Sub SetTB()  
        Dim tot As Decimal = 0D  
        For Each c As Control In GroupBox4.Controls  
            If c.GetType = GetType(TextBox) Then  
                tot += GetDec(c.Text)  
            End If  
        Next  
  
        'If TxtWaitingTime.Text = Nothing Then  
        '    TxtWaitingTime.Text = "0.00"  
        'ElseIf TxtTolls.Text = Nothing Then  
        '    TxtTolls.Text = "0.00"  
        'End If  
  
        TxtWaitingTime.Text = GetDec(TxtWaitingTime.Text).ToString + TxtTolls.Text = GetDec(TxtTolls.Text).ToString("£0.00")  
  
        LbNet.Text = GetDec(TxtWaitingTime.Text) + GetDec(TxtTolls.Text) + tot.ToString("£0.00")  
  
  
    End Sub  

With this code I keep getting the wrong answer or the TxtWaitingTime puts a value of " False "

Please can any of you great People help.

Kind Regards
Gary

181064-vb-question-on-sum.png

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,141 Reputation points
    2022-03-09T11:04:35.577+00:00

    Hi

    You are finding the intricacies of writing code :) The issue as I understand it is that you are trying to add to a final calculation. This is always something I would avoid. My approach in such a case is to include the two values in the main calculation having initially set them to zeroes first. During other data being calculated, with these being zero, the Net total is the same as though they were not there. Now, when the values are changed in the TextBoxes, do the entire calculation again - this means the Net value will always be correct.
    Of course, if you do not already have a Sub of some sort that you use for overall calculation, it would mean alterations.
    It would probably be worth the effort, as the only other way I can think of is that you track every TextBox change and 'UNDO' the changes as needed - this is NOT a trivial way to go,
    Now, the question is: do you have a common Calculation Sub?

    Here is a sample of what I am saying:

    181474-111.png

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
     ' add a few values - these would be your  
     ' own calculated values or user input.  
     TextBox1.Text = 0.ToString("c2")  
     TextBox2.Text = 0.ToString("c2")  
     TextBox3.Text = (1.45).ToString("c2")  
     TextBox4.Text = (2.45).ToString("c2")  
     TextBox5.Text = (3.55).ToString("c2")  
     TextBox6.Text = (4.55).ToString("c2")  
     Calc()  
     End Sub  
     Sub Calc()  
     Dim tot As Decimal = 0.0D  
     For Each gbc As Control In Controls  
     If gbc.GetType = GetType(GroupBox) Then  
     For Each c As Control In gbc.Controls  
     If c.GetType = GetType(TextBox) Then  
     tot += GetDecimal(c.Text.Replace("£"c, String.Empty))  
     End If  
     Next  
     End If  
     Next  
     Label2.Text = tot.ToString("c2")  
     End Sub  
     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged  
     Calc()  
     End Sub  
     Function GetDecimal(s As String) As Decimal  
     Dim v As Decimal = 0.0D  
     If Decimal.TryParse(s, v) Then Return v  
     Return 0.0D  
     End Function  
    End Class  
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. LesHay 7,141 Reputation points
    2022-03-08T19:07:42.74+00:00

    Hi
    Here is an example. Here, use a Label with Net amount (string value), and add both values from 2 textboxes below. However, I suspect that you will not be happy doing this. The values are constantly being added to every time a TextBox changes. I feel you would be better to have a 'Calc' button once values are entered, and, that you repeat the entire calculation - otherwise the point about constant changes will occur (slightly differently)

    181084-111.png

    Public Class Form1  
     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged  
     Dim tot As Decimal = 0.00  
     For Each c As Control In GroupBox4.Controls  
     If c.GetType = GetType(TextBox) Then  
     tot += GetDecimal(c.Text)  
     End If  
     Next  
     Dim labVal As Decimal = GetDecimal(Label2.Text.Replace("£"c, String.Empty))  
     Label2.Text = (labVal + tot).ToString("£0.00")  
    ' or,  Label2.Text = (labVal + tot).ToString("c2")  
     End Sub  
     Function GetDecimal(s As String) As Decimal  
     Dim v As Decimal = 0.00  
     If Decimal.TryParse(s, v) Then Return v  
     Return 0.00  
     End Function  
    End Class  
    
    0 comments No comments

  2. Gary Simpson 471 Reputation points
    2022-03-09T00:01:29.637+00:00

    Hi LesHay

    Thank you for getting back to me. I have tried your code naming my own labels and textboxes.
    And you are right there is a slight problem. As I enter number(s) / Numeric values and even decimal point adds to my Label ( LbNet )

    So I tried your suggestion and used a Button. Then your code you kindly wrote works.
    the only trouble is, If a user has entered number(s) / Numeric values, And then presses the button, then the user realizes he has entered a wrong Value
    in one or both textboxes. it is then I am stuck because the value of the Label ( LbNet ) has changed to the new total.

    at first every time I clicked the button it just kept on adding to the total, So after clicking the button I disabled it.

    Your code I have now with the alterations .

    Private Sub CmdCalculate_Click(sender As Object, e As EventArgs) Handles CmdCalculate.Click
            Dim tot As Decimal = 0.00
            For Each c As Control In GroupBox4.Controls
                If c.GetType = GetType(TextBox) Then
                    tot += GetDecimal(c.Text)
                End If
            Next
            Dim labVal As Decimal = GetDecimal(LbNet.Text.Replace("£"c, String.Empty))
            LbNet.Text = (labVal + tot).ToString("c2")
            ' or,  Label2.Text = (labVal + tot).ToString("c2")
        End Sub
        Function GetDecimal(s As String) As Decimal
            Dim v As Decimal = 0.00
            If Decimal.TryParse(s, v) Then Return v
            Return 0.00
        End Function
    

    Regards
    Gary

    PS. would it help if I just used Textboxes instead of Labels?

    0 comments No comments

  3. Gary Simpson 471 Reputation points
    2022-03-09T15:54:17.217+00:00

    Hi LesHay

    Thanks again for taking the time and trouble to look at my question and writing out the code.

    I tried your code altering your code to my Textboxes. ( I have exchange Labels for textboxes ) And the sum did not seem to change net value.
    But I have written a simple code for the sum that works now.

    Code I Have now.

    Sub Calc()  
      
            Dim Sum1 As Decimal = GetDec(TxtSubTotal.Text)  
            Dim Sum2 As Decimal = GetDec(TxtVAT.Text)  
            Dim Sum3 As Decimal = GetDec(TxtWaitingTime.Text)  
            Dim Sum4 As Decimal = GetDec(TxtTolls.Text)  
      
            TxtNet.Text = Sum1 + Sum2 + Sum3 + Sum4  
            TxtNet.Text = FormatCurrency(TxtNet.Text)  
        End Sub  
    
        Private Sub TxtWaitingTime_TextChanged(sender As Object, e As EventArgs) Handles TxtWaitingTime.TextChanged, TxtTolls.TextChanged  
            Calc()  
        End Sub  
        Function GetDecimal(s As String) As Decimal  
            Dim v As Decimal = 0.0D  
            If Decimal.TryParse(s, v) Then Return v  
            Return 0.0D  
        End Function  
      
        Private Sub TxtWaitingTime_Leave(sender As Object, e As EventArgs) Handles TxtWaitingTime.Leave  
            If String.IsNullOrWhiteSpace(TxtWaitingTime.Text) = True Then  
                TxtWaitingTime.Text = "0.00"  
            ElseIf String.IsNullOrWhiteSpace(TxtWaitingTime.Text) = False Then  
                'do nothing just show value  
                TxtWaitingTime.Text = FormatCurrency(TxtWaitingTime.Text)  
            End If  
        End Sub  
      
        Private Sub TxtTolls_Leave(sender As Object, e As EventArgs) Handles TxtTolls.Leave  
            If String.IsNullOrWhiteSpace(TxtTolls.Text) = True Then  
                TxtTolls.Text = "0.00"  
            ElseIf String.IsNullOrWhiteSpace(TxtTolls.Text) = False Then  
                'do nothing just show value  
                TxtTolls.Text = FormatCurrency(TxtTolls.Text)  
            End If  
        End Sub  
    

    Kindest Regards
    Gary

    When Data is selected
    181487-for-leshay1.png

    After entering value into textbox
    181515-for-leshay2.png

    after entering value into second textbox
    181496-for-leshay3.png

    0 comments No comments

  4. LesHay 7,141 Reputation points
    2022-03-09T16:34:37.283+00:00

    Hi
    Here is a slightly better version, doing away with the Leave event handler altogether. As usual with all the code I post, always needs proper testing and perhaps exception handling as the snippets I post are 'as is' and have limited testing and little or no exception handling.
    This version seems to be OK as far as I have tested. and reduces the code somewhat.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = FormatCurrency(0)
            TextBox2.Text = FormatCurrency(0)
            TextBox3.Text = FormatCurrency(1.45)
            TextBox4.Text = FormatCurrency(2.55)
            TextBox5.Text = FormatCurrency(3.55)
            TextBox6.Text = FormatCurrency(4.45)
            Calc()
        End Sub
        Sub Calc()
            Dim tot As Decimal = 0.0D
            For Each gbc As Control In Controls
                If gbc.GetType = GetType(GroupBox) Then
                    For Each c As Control In gbc.Controls
                        If c.GetType = GetType(TextBox) Then
                            tot += GetDecimal(c.Text.Replace("£"c, String.Empty))
                            AddHandler c.TextChanged, AddressOf TextBox_TextChanged
                        End If
                    Next
                End If
            Next
            Label2.Text = FormatCurrency(tot)
        End Sub
        Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)
            Dim c As TextBox = DirectCast(sender, TextBox)
            Dim pos As Integer = c.SelectionStart
            Dim len As Integer = c.SelectionLength
            c.Text = FormatCurrency(GetDecimal(c.Text.Replace("£"c, String.Empty)))
            c.SelectionStart = pos
            c.SelectionLength = len
            Calc()
        End Sub
        Function GetDecimal(s As String) As Decimal
            Dim v As Decimal = 0.0D
            If Decimal.TryParse(s, v) Then Return v
            Return 0.0D
        End Function
    End Class
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.