# Sum of all text boxes TextChanged.

• Jan 3rd, 2018, 06:20 AM
Interplain
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```
• Jan 3rd, 2018, 06:40 AM
OptionBase1
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
• Jan 3rd, 2018, 06:42 AM
si_the_geek
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")`
• Jan 3rd, 2018, 06:57 AM
jmcilhinney
Re: Sum of all text boxes TextChanged.
Quote:

Originally Posted by OptionBase1
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.
• Jan 3rd, 2018, 09:41 AM
OptionBase1
Re: Sum of all text boxes TextChanged.
Quote:

Originally Posted by jmcilhinney
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 :eek:
• Jan 3rd, 2018, 03:01 PM
dday9
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```
• Jan 3rd, 2018, 07:40 PM
jmcilhinney
Re: Sum of all text boxes TextChanged.
Quote:

Originally Posted by dday9
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.