Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 20th, 2006, 10:56 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default textboxes and additions


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+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.
tg.

Reply With Quote
  #2 (permalink)  
Old January 20th, 2006, 11:13 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default


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 With Quote
  #3 (permalink)  
Old January 21st, 2006, 06:09 PM
Authorized User
 
Join Date: Dec 2005
Location: San Nicolas, Buenos Aires, Argentina.
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
Default

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 With Quote
  #4 (permalink)  
Old January 23rd, 2006, 02:45 AM
Registered User
 
Join Date: Jan 2006
Location: chennai, Tamil Nadu, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sarbjitmultani
Default

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,
sarbjit
Reply With Quote
  #5 (permalink)  
Old January 23rd, 2006, 11:50 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:15 PM.


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