Add values from two textboxes and display the sum

2019-03-31 09:24发布

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?

4条回答
狗以群分
2楼-- · 2019-03-31 10:04

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();
 }
查看更多
地球回转人心会变
3楼-- · 2019-03-31 10:09

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)));
        }
    }
查看更多
成全新的幸福
4楼-- · 2019-03-31 10:14

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();
         }
查看更多
闹够了就滚
5楼-- · 2019-03-31 10:19

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.

查看更多
登录 后发表回答