当前位置: 首页 > 编程语言 > C#

两个文本框的值相加,第三个文本框显示和分享

时间:2023-04-10 17:03:54 C#

两个文本框的值相加,第三个文本框显示和这段代码是从textbox1添加的.text和textbox2.text到textbox3.textprivatevoidtextBox1_TextChanged(objectsender,EventArgse){if(!string.IsNullOrEmpty(textBox1.Text)||string.IsNullOrEmpty(textBox2.Text)){textBox3.Text=(Convert.ToInt32(textBox1.Text)+Convert.ToInt32(textBox2.Text).ToString());}}privatevoidtextBox2_TextChanged(objectsender,EventArgse){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());试试这个经过测试的代码。privatevoidtextBox1_TextChanged(objectsender,EventArgse){if(!string.IsNullOrEmpty(textBox1.Text)&&!string.IsNullOrEmpty(textBox2.Text))textBox3.Text=(Convert.ToInt32(textBox1.Text)+Convert.ToInt32(textBox2.Text)).ToString();}privatevoidtextBox2_TextChanged(objectsender,EventArgse){if(!string.IsNullOrEmpty(textBox1.Text)&&!string.IsNullOrEmpty(textBox2.Text))textBox3.Text=(Convert.ToInt32(textBox1.Text)+转换。ToInt32(textBox2.Text)).ToString();您当前的表达式在条件的第二部分缺少否定(!)此外,它应该是&&而不是||至于你的错误,字符串格式不正确,只要输入的字符串不能转换为int,你就会得到任何不安全的代码。用trycatch包围它或使用Int32.TryParse:privatevoid**textBox_TextChanged**(objectsender,EventArgse){intfirst=0;秒数=0;if(Int32.TryParse(textBox2.Text,outsecond)&&Int32.TryParse(textBox1.Text,outfirst))textBox3.Text=(first+second).ToString();}}顺便说一句,就像格伦指出的那样,你只能使用一个事件处理程序,就像这个例子一样。你可以这样做:if(!string.IsNullOrEmpty(textBox1.Text)||string.IsNullOrEmpty(textBox2.Text)){.Text)).toString();}用这个。以上是C#学习教程:将两个文本框中的值相加,在第三个文本框中显示所有共享的内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注—privatevoidtextBox2_TextChanged(objectsender,EventArgse){if(!string.IsNullOrEmpty(textBox1.Text)&&!string.IsNullOrEmpty(textBox2.Text)){textBox3.Text=Convert.ToString((Convert.ToInt32(textBox1.Text)+Convert.ToInt32(textBox2.Text)));}}privatevoidtextBox1_TextChanged(objectsender,EventArgse){if(!string.IsNullOrEmpty(textBox1.Text)&&!string.IsNullOrEmpty(textBox2.Text)){textBox3.Text=Convert.ToString((Convert.ToInt32(textBox1).Text)+Convert.ToInt32(textBox2.Text)));}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: