Results 1 to 7 of 7

Thread: Sum of all text boxes TextChanged.

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2017
    Posts
    36

    Post Sum of all text boxes TextChanged.

    Can anyone help with this please. This works OK with totaling up what ever is put in the textboxes. But what i can figure is that when leaving the text box it rounds it up. if i put 1.5 in it rounds it to 2 removing the .0 i have tried formatting string. I want it to show 1.5
    Code:
    Public Sub PalletQTY_TextChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles PalletQTYTextBox.TextChanged, PalletQTYTextBox2.TextChanged, PalletQTYTextBox3.TextChanged, PalletQTYTextBox4.TextChanged, PalletQTYTextBox5.TextChanged
    
    
            Try
    
    
                Dim Result As Decimal
                Dim Operand1, Operand2, Operand3, Operand4, Operand5 As Decimal
    
    
                ' Try and convert the text in the first textbox to a Single.
                Try
                    Operand1 = Convert.ToDecimal(PalletQTYTextBox.Text)
                Catch ex As Exception
                    Operand1 = 0
                End Try
    
    
                ' Try and convert the text in the second textbox to a Single.
                Try
                    Operand2 = Convert.ToDecimal(PalletQTYTextBox2.Text)
                Catch ex As Exception
                    Operand2 = 0
                End Try
    
    
                Try
                    Operand3 = Convert.ToDecimal(PalletQTYTextBox3.Text)
                Catch ex As Exception
                    Operand3 = 0
                End Try
    
    
                ' Try and convert the text in the second textbox to a Single.
                Try
                    Operand4 = Convert.ToDecimal(PalletQTYTextBox4.Text)
    
    
                Catch ex As Exception
                    Operand4 = 0
                End Try
    
    
                ' Try and convert the text in the second textbox to a Single.
                Try
                    Operand5 = Convert.ToDecimal(PalletQTYTextBox5.Text)
                Catch ex As Exception
                    Operand5 = 0
                End Try
    
    
    
    
                Result = Operand1 + Operand2 + Operand3 + Operand4 + Operand5
                labTotal.Text = Result.ToString
    
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
    
        End Sub

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Sum of all text boxes TextChanged.

    Like Integer or Long, the Decimal data type stores integers only.

    If you need numbers with values after the decimal point, use Single or Double.

    https://docs.microsoft.com/en-us/dot...-numeric-types

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Sum of all text boxes TextChanged.

    That code is very long-winded, it can be shortened quite a bit by using Decimal.TryParse instead of those Try/Catch blocks, and even more by using an array for the textbox values, ie:
    Code:
    Public Sub PalletQTY_TextChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles PalletQTYTextBox.TextChanged, PalletQTYTextBox2.TextChanged, PalletQTYTextBox3.TextChanged, PalletQTYTextBox4.TextChanged, PalletQTYTextBox5.TextChanged
    
            Try
                Dim Result As Decimal = 0 
                Dim textItems as String() = {PalletQTYTextBox.Text, PalletQTYTextBox2.Text, PalletQTYTextBox3.Text,
                                             PalletQTYTextBox4.Text, PalletQTYTextBox5.Text}
    
                For Each textItem in textItems
                    ' Try and convert the text to a Decimal.
                    Dim tempDecimal as Decimal
                    If Decimal.TryParse(textItem, tempDecimal) Then
                        Result += tempDecimal
                    End If
                Next
    
                labTotal.Text = Result.ToString
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub
    If you switch from Textboxes to NumericUpdowns then much more code can be removed, as you would only need two lines (add the values, and display). This would also eliminate the possibility of the user entering invalid values, and issues from converting text to numeric.


    In terms of your issue, you should be specifying a format with .ToString, eg:
    Code:
                labTotal.Text = Result.ToString("G")
    Last edited by si_the_geek; Jan 3rd, 2018 at 08:01 AM. Reason: typo

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sum of all text boxes TextChanged.

    Quote Originally Posted by OptionBase1 View Post
    Like Integer or Long, the Decimal data type stores integers only.
    Nope, that's quite wrong. I suggest that you follow the link you provided and read it again.

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Sum of all text boxes TextChanged.

    Quote Originally Posted by jmcilhinney View Post
    Nope, that's quite wrong. I suggest that you follow the link you provided and read it again.
    Thanks, I'm going to have to quit posting when I just wake up

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Sum of all text boxes TextChanged.

    I like the suggestions that si_the_geek provided, however I'd like to extend them to the following:
    • You should absolutely be using a NumericUpDown control, it would greatly eliminate most of the code
    • You should be using the ValueChanged event of the NumericUpDown controls, but if you stayed with the TextBox then you should use either the Validating or Validated event instead of the TextChanged event
    • You could use the Sum method on the collection of values to eliminate the manual math


    Take a look at this example that assumes you decide to use NUDs:
    Code:
    Private Sub NumericUpDowns_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles PalletQTYNud.ValueChanged, PalletQTYNud2.ValueChanged, PalletQTYNud3.ValueChanged, PalletQTYNud4.ValueChanged, PalletQTYNud5.ValueChanged
        'Get all of the NUD values
        Dim values() As Decimal = {PalletQTYNud.Value, PalletQTYNud2.Value, PalletQTYNud3.Value, PalletQTYNud4.Value, PalletQTYNud5.Value}
    
        'Sum all of the values and display them in the Label
        labTotal.Text = values.Sum().ToString()
    End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sum of all text boxes TextChanged.

    Quote Originally Posted by dday9 View Post
    if you stayed with the TextBox then you should use either the Validating or Validated event instead of the TextChanged event
    I'd suggest that they should be either using both Validating and Validated or neither and using Leave instead. Validating would be used to test whether the contents was valid and prevent the control losing focus if it wasn't and Validated would be where the data, which would then be known to be valid, would be used. If you didn't want to prevent the control losing focus if the data was invalid then you'd handle Leave and validate the data there and use it if and only if it was valid.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width