Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 20th, 2006, 10:56 AM
Registered User
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

Old January 20th, 2006, 11:13 AM
Registered User
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

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?

Old January 21st, 2006, 06:09 PM
Authorized User
Join Date: Dec 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to arielote Send a message via MSN to arielote Send a message via Yahoo to arielote

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
Old January 23rd, 2006, 02:45 AM
Registered User
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sarbjitmultani

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,
Old January 23rd, 2006, 11:50 AM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

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.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding Textboxes RubyRue ASP.NET 1.0 and 1.1 Basics 1 February 18th, 2008 11:13 AM
No additions using VBA and SQL Bob Pierce Access VBA 1 February 6th, 2007 01:20 PM
Two TextBoxes hunzian78@yahoo.com ASP.NET 1.0 and 1.1 Basics 5 April 1st, 2006 06:32 AM
Showing 5 latest additions, problem with sorting Konfu XSLT 0 March 11th, 2006 10:21 AM
Access 2000 "Open ODBC SQL Table for Additions" cjdphlx Access VBA 3 July 18th, 2005 02:05 PM

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