加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从两个文本框中添加值,并在第三个文本框中显示总和

发布时间:2020-12-16 00:19:14 所属栏目:百科 来源:网络整理
导读:我已尝试将此代码从textbox1.text和textbox2.text添加到textbox3.text中 private void textBox1_TextChanged(object sender,EventArgs e) { if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text)) { textBox3.Text = (Convert.T
我已尝试将此代码从textbox1.text和textbox2.text添加到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());
        }
    }

请帮助…有没有什么比将文本框的’格式’属性更改为一般数字?

解决方法

你弄错了||应替换为&&所以它会检查两个文本框都填充了值.

您错误地使用了.ToString()方法,该方法仅适用于textbox2,请正确检查括号.

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

应该

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

试试这个经过测试的代码.

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();
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读