Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

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
0
zstafa
Asked:
zstafa
1 Solution
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now