Add values from two textboxes and display the sum

2019-03-31 09:32发布

问题:

I have tried this code to add from textbox1.text and textbox2.text into textbox3.text

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

         { 
             textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());
         }
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

        {
        textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());
        }
    }

please Help ... and is there anything like to change 'format 'property of textbox to general number?

回答1:

You made a mistake || should be replace with && so it will check both the text boxes are filled with value.

You have used .ToString() method wrongly which applies only to textbox2, check the brackets properly.

textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());

should be

textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString());

Try This Tested Code.

 private void textBox1_TextChanged(object sender, EventArgs e)
 {
  if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
  textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString();
 }

 private void textBox2_TextChanged(object sender, EventArgs e)
 {
  if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
  textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString();
 }


回答2:

Your current expression is missing the negation (!) in the second part of your condition

Also, it should be a && not ||

As for your error, string was not in the correct format, you will get this with any unsafe code whenever the input string cannot be converted to an int. Surround it with a try catch or use Int32.TryParse:

private void **textBox_TextChanged**(object sender, EventArgs e)
{
     int first = 0;
     int second= 0;
     if(Int32.TryParse(textBox2.Text, out second) && Int32.TryParse(textBox1.Text, out first))
         textBox3.Text = (first + second ).ToString();
     }
}

Btw, like Glenn has pointed out, you can use only one event handler like in this example.



回答3:

you can do like this:

if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

         { 

             textBox3.Text =convert.toString(Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).toString();
         }


回答4:

use this.

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
        {
            textBox3.Text = Convert.ToString((Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)));
        }
    }
private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
        {
            textBox3.Text = Convert.ToString((Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)));
        }
    }