Sabir Ali Khuhro Sabir Ali Khuhro - 2 months ago 9
C# Question

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

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

Answer

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.