By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
461,991 Members | 483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 461,991 IT Pros & Developers. It's quick & easy.

How to add Multiple textbox values as they are updated?

100+
P: 110
I have a several text boxes available on a form. At the bottom of the form there is a disabled text box for the total. I currently can add all the values upon a button click, however I would like to, if possible, make it so as a form is filled in, the total adds within the total text box.

I thought maybe something like:
Expand|Select|Wrap|Line Numbers
  1. txtbxTotal.text = txtbxTotal.Text + (list of other text boxes)
  2.  
I am running into two issues, I don't know how if at all I can code it to increment in real time as the fields are updated, and I also don't know how to do something like a try parse when I am accepting decimal values.

Any help with either problem are greatly appreciated.
Jan 8 '11 #1
Share this Question
Share on Google+
3 Replies

100+
P: 110
With regards to adding the values of decimals and verifying that they contain only numbers and a single decimal, I call a try on my method that adds the values, and a catch on the format error exception and that allows me to make sure it is acceptable numbers being used.

Still need help with the live update in the total text box though.
Jan 9 '11 #2

GaryTexmo
Expert 100+
P: 1,501
There is an event on a TextBox called TextChanged...

http://msdn.microsoft.com/en-us/libr...xtchanged.aspx

You can create a handler for this event where you call a method that will check to see if the textboxes have values... if they do, they enable the total and then perform the calculation, otherwise it disables the total text box.

For parsing doubles, you can use the TryParse method available on the double type.

http://msdn.microsoft.com/en-us/library/3s27fasw.aspx
Jan 10 '11 #3

100+
P: 110
Awesome. This is what I ended up with in case anyone in the future wants an example:

Expand|Select|Wrap|Line Numbers
  1. private void txtbxOnes_TextChanged(object sender, EventArgs e)
  2.         {
  3.             try
  4.             {
  5.                 AddValues();
  6.             }
  7.             catch (FormatException)
  8.             {
  9.                 //do nothing
  10.             }
  11.         }
  12.  
Expand|Select|Wrap|Line Numbers
  1. if (txtbxOnes.Text == "")
  2.                 ones = 0;
  3.             else ones = Convert.ToInt32(txtbxOnes.Text);
  4.  
  5. txtbxTotal.Text = ones; //and then whatever other text box values are needed.
  6.  
Thanks textmo

I will look at the tryparse for doubles, I ended up using a try catch on the addvalues() method that catches a format error that gets kicked up if a letter or symbol is used.
Jan 10 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.