Link to home
Start Free TrialLog in
Avatar of Whing Dela Cruz
Whing Dela CruzFlag for Anguilla

asked on

Sum Total of two TextBox

How to add the following TextBox?

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

+ doesn't work...
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of 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
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fast and Perfect... Thanks a lot!