Solved

Sum Total of two TextBox

Posted on 2012-03-14
4
322 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
[X]
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
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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