|
Subject:
|
textboxes and additions
|
|
Posted By:
|
toiletgraffiti
|
Post Date:
|
1/20/2006 9:56:33 AM
|
Hi
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+txtShare4.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. tg.
|
|
Reply By:
|
toiletgraffiti
|
Reply Date:
|
1/20/2006 10:13:48 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? tg.
|
|
Reply By:
|
arielote
|
Reply Date:
|
1/21/2006 5:09:07 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?
Regards
Arielote
--- Ariel Folonier San Nicolas Argentina
|
|
Reply By:
|
sarbjitmultani
|
Reply Date:
|
1/23/2006 1:45:15 AM
|
Dear toiletgraffiti ,
Kindly check the following code for your problem.
textBox5.Text=Convert.ToString(Convert.ToInt32(textBox1.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, sarbjit
|
|
Reply By:
|
planoie
|
Reply Date:
|
1/23/2006 10:50:32 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.
-Peter
|