p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   textboxes and additions (http://p2p.wrox.com/showthread.php?t=37441)

toiletgraffiti January 20th, 2006 10:56 AM

textboxes and additions


Im having difficulty figuring out how to sum the values a user enters into 5 textboxes. I want a total to appear in a 'total' textbox. Its a windows application in c#.

My code so far is:

txtTotal.Text = (txtShare1.Text+txtShare2.Text+txtShare3.Text+txtS hare4.Text+txtShare5.Text);

All this does is concatenate the vlaues rather than summing them...Im sure this is very simple I just cant figure out the syntax!

Any help greatly appreciated.

toiletgraffiti January 20th, 2006 11:13 AM

ok..I did this:

int theSum = Int32.Parse(txtShare1.Text) + Int32.Parse(txtShare2.Text) + Int32.Parse(txtShare3.Text) + Int32.Parse(txtShare4.Text) + Int32.Parse(txtShare5.Text);

txtTotal.Text = theSum.ToString();

which *should* work okay? however Im now getting the following: Input string was not in a correct format.

any ideas?

arielote January 21st, 2006 06:09 PM

I've got same problem comparing strings with <= .
In old "C" you've got "atoi" or "atof" functions, but I do not know waht happend in this new version.
However in VB normally string got property ste.Value wich returns numeric value.

Do somebody know an aeticle about types convertions, o related functions?



Ariel Folonier
San Nicolas

sarbjitmultani January 23rd, 2006 02:45 AM

Dear toiletgraffiti ,

Kindly check the following code for your problem.

textBox5.Text=Convert.ToString(Convert.ToInt32(tex tBox1.Text.ToString())+ Convert.ToInt32(textBox2.Text.ToString())+Convert. ToInt32(textBox3.Text.ToString())+Convert.ToInt32( textBox4.Text.ToString()));

Just change the textbox name whatever you have given in your application.
take care

Best regards,

planoie January 23rd, 2006 11:50 AM

Is it possible you are experiencing a problem because the code is encountering an empty textbox? "" is probably causing the "invalid format" exception because an empty string is not numeric. Try creating a helper function that accepts the textbox as an argument and checks for an empty .Text property, returns 0 for that or returns the property value converted to an integer. Then just call the function for all textboxes you are going to add together.


All times are GMT -4. The time now is 10:43 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.