Avatar of Whing Dela Cruz
Whing Dela Cruz
Flag for Anguilla asked on

Sum Total of two TextBox

How to add the following TextBox?

txtTotal.Text = txtTotal.Text + txtTop.Text;

+ doesn't work...
C#

Avatar of undefined
Last Comment
Whing Dela Cruz

8/22/2022 - Mon
kaufmed

You must convert the textual values to a numeric type in order to perform arithmetic on them. I typically suggest using one of the TryParse methods for this because you get validation that the values actually represent numbers before trying to work arithmetic against them. For example:

decimal decTotal = 0;
decimal decTop = 0;

if (decimal.TryParse(txtTotal.Text, out decTotal) &&
    decimal.TryParse(txtTop.Text, out decTop))
{
    txtTotal.Text = (decTotal + decTop).ToString();
}
else
{
    MessageBox.Show("Invalid value(s)!");
}

Open in new window

Whing Dela Cruz

ASKER
Thanks a lot Kaufmed, you gave perfect solution which I spent a lot of time... but could you extent your help? I need to have format for this like this 12,440.83
ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Whing Dela Cruz

ASKER
Fast and Perfect... Thanks a lot!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23