Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sum Total of two TextBox

Posted on 2012-03-14
4
318 Views
Last Modified: 2012-03-14
How to add the following TextBox?

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

+ doesn't work...
0
Comment
Question by:Whing Dela Cruz
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37718854
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

0
 

Author Comment

by:Whing Dela Cruz
ID: 37718925
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
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37719085
Pass a "format string" to the ToString call:

...

txtTotal.Text = (decTotal + decTop).ToString("#,##0.00");

...

Open in new window


The #'s mean an optional field in the output; the 0's mean a mandatory digit in the output (i.e. if there's no valid digit to fill that position in the original value, then a zero is output for that position). If your sum were .2, then you would see 0.20 in the output. If the sum were 2000, then you would see 2,000.00 in the output.

I go here for my format strings:  http://blog.stevex.net/string-formatting-in-csharp/
0
 

Author Closing Comment

by:Whing Dela Cruz
ID: 37719107
Fast and Perfect... Thanks a lot!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question