C# 从两个文本框中添加值并在第三个文本框中显示总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9394909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add values from two textboxes and display the sum in third textbox
提问by HMcompfreak
I have tried this code to add from textbox1.text and textbox2.text into textbox3.text
我试过这段代码从 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());
}
}
please Help ... and is there anything like to change 'format 'property of textbox to general number?
请帮助...是否有类似将文本框的“格式”属性更改为通用数字的方法?
采纳答案by mr_eclair
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.
您.ToString()错误地使用了仅适用于 的方法textbox2,请正确检查括号。
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();
}
回答by Orkun Ozen
Your current expression is missing the negation (!) in the second part of your condition
您当前的表达式缺少条件的第二部分中的否定 (!)
Also, it should be a &¬ ||
此外,它应该&&不是||
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 catchor use
Int32.TryParse:
至于您的错误,string 的格式不正确,只要输入字符串无法转换为int,您就会使用任何不安全的代码获得此错误。用 atry catch或 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.
顺便说一句,就像 Glenn 指出的那样,您只能像本示例中那样使用一个事件处理程序。
回答by Mulesoft Developer
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();
}
回答by Maganjo
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)));
}
}

