How do I add the contents of 3 textboxes when the value of any one of them change and put the solution in another textbox?

How do I add the contents of 3 textboxes when the value of any one of them change and put the updated solution in a 4th textbox? I have it working but I get this error

"Input string was not in the correct format"

I am converting the values to integers and then back to a string. An explanation or solution would help. Thanks
LVL 3
zstafaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsaboCommented:
Why are you converting the values to integer?

You could implement something like this.

handle the textchanged event in each of the 3 textbox.

textBox1.TextChanged += new EventHandler(textBox1_TextChanged);


private void textBox1_TextChanged(Object sender, EventArgs e)
{
      textBox4.Text = textBox1.Text;      
}

if you want to convert to int, you must use a try catch in case of a bad input.

private void textBox1_TextChanged(Object sender, EventArgs e)
{
      try
      {
            int x = int.Parse(textBox1.Text);
            textBox4.Text = textBox1.Text;  // you could convert the value back to string but doesn't make any sense     textBox4.Text = x.ToString();
      }
      catch
      {
            MessageBox.Show("Bad Input");
      }
      textBox4.Text = textBox1.Text;      
}

hope this helps, DS


0
zstafaAuthor Commented:
I convert to integers because I am adding the contents of textbox1, textbox2, and textbox3. So textbox4= (textbox1 + textbox2 + textbox3). The contents of all of the textboxes are numbers. Thanks for you help.
0
jatinderalaghCommented:
Try this

this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox2.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox3.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

private void textBox1_TextChanged(object sender, System.EventArgs e)
            {
                  int T1 = 0,T2 = 0,T3 =0;
                  try
                  {
                        if(!textBox1.Text.Equals(string.Empty))
                        {
                              T1 = int.Parse(textBox1.Text);
                        }
                        if(!textBox2.Text.Equals(string.Empty))
                        {
                              T2 = int.Parse(textBox2.Text);
                        }
                        if(!textBox3.Text.Equals(string.Empty))
                        {
                              T3 = int.Parse(textBox3.Text);
                        }
                  }
                  catch
                  {// Empty catch block u can use for displaying incorrect input messages}
                  int total = T1+T2+T3;
                  textBox4.Text = total.ToString();
            }
            
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.