-3

enter image description here

Add values from two textboxes and display the sum in third textbox 3 but value of 1st aor 2nd textbox show in third textbox

4
  • Unclear what you try to achieve, do you want the program to add textbox1's value and textbox2's value and show the sum in textbox3?
    – Roy123
    Sep 23, 2016 at 12:00
  • yes but when i enter any number in 1st textbox this number automatically show in third textbox then add with 2nd number Sep 23, 2016 at 12:04
  • 1
    you can use a textchanged event to let the program do this automatically when you change the text in your textbox. Put @Imad his code inside that event.
    – Roy123
    Sep 23, 2016 at 12:09
  • please send me any sample? Sep 23, 2016 at 12:36

2 Answers 2

1
TextBox3.Text = Convert.ToDouble(TextBox1.Text) + Convert.ToDouble(TextBox2.Text)

This will do it, just enter proper number. No exception handled.

To handle exception, use Double.TryParse instead of Convert.ToDouble.

Look at msdn example.

2
  • yes but when i enter any number in 1st textbox this number automatically show in third textbox then add with 2nd number Sep 23, 2016 at 12:07
  • then how we add exception? Sep 23, 2016 at 12:57
0

This will automatically change your value once you alter textbox1's value

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0)
        {
            textBox3.Text = Convert.ToString(Convert.ToDouble(textBox1.Text) + Convert.ToDouble(textBox2.Text));
        }
        if (textBox1.Text.Length > 0 && textBox2.Text.Length == 0)
        {
            textBox3.Text = textBox1.Text;
        }
        if (textBox1.Text.Length == 0 && textBox2.Text.Length > 0)
        {
            textBox3.Text = textBox2.Text;
        }
        if(textBox1.Text.Length == 0 && textBox2.Text.Length == 0)
        {
            textBox3.Text = "0";
        }
    }

edit: treat textbox value as 0 when its empty.

This example will still error if you enter non-numeric values into the textbox.

6
  • this show error Error Cannot implicitly convert type 'double' to 'string' Sep 23, 2016 at 12:42
  • yes it gives an error when either one was empty then what i do next sir?? Sep 23, 2016 at 13:28
  • Updated code to handle the formatException error, excuse me for being sloppy in my coding. I've made myself an application to test this and it works now :)
    – Roy123
    Sep 23, 2016 at 14:02
  • now it is true but can't add number in third textbox Sep 23, 2016 at 14:20
  • It didn't say you wanted to do this in your question, edit your question in order for people to answer it to your desire.
    – Roy123
    Sep 23, 2016 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.