C# 将两个文本框的值相乘并在第三个文本框中显示结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17664576/
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
Multiplication of the values of two textboxes and displaying result in the third textbox
提问by Haider Khattak
i am developing an inventory system in which i fetch the data from database through textbox1 and the data compatible to the entry in textbox1 appears in textbox8 and textbox10 which are integers. Now i want to multiply textbox8 and textbox10 and the result should be display into textbox7 when the integers appears in textbox8 and textbox10.
我正在开发一个库存系统,其中我通过 textbox1 从数据库中获取数据,并且与 textbox1 中的条目兼容的数据出现在 textbox8 和 textbox10 中,它们是整数。现在我想将 textbox8 和 textbox10 相乘,当整数出现在 textbox8 和 textbox10 中时,结果应该显示到 textbox7 中。
i am using this piece of code for multiplication of two textboxes and result in the third text box does not appear:
我正在使用这段代码将两个文本框相乘,结果第三个文本框没有出现:
private void textBox7_TextChanged(object sender, EventArgs e)
{
Int32 val1 = Convert.ToInt32(textBox10.Text);
Int32 val2 = Convert.ToInt32(textBox8.Text);
Int32 val3 = val1 * val2;
textBox7.Text = val3.ToString();
}
采纳答案by pcnThird
You assigned the event to the wrong control and you're not checking for valid input. You can use int.TryParseto look for valid, numerical input.
您将事件分配给了错误的控件,并且您没有检查有效输入。您可以使用它int.TryParse来查找有效的数字输入。
private void textBox8_TextChanged(object sender, EventArgs e)
{
Multiply();
}
private void textBox10_TextChanged(object sender, EventArgs e)
{
Multiply();
}
public void Multiply()
{
int a, b;
bool isAValid = int.TryParse(textBox8.Text, out a);
bool isBValid = int.TryParse(textBox10.Text, out b);
if (isAValid && isBValid)
textBox7.Text = (a * b).ToString();
else
textBox7.Text = "Invalid input";
}
回答by dwonisch
You are using the wrong textbox for your TextChanged event. Your textboxes 8 and 10 are changing but you have attached the shown code to textbox7.
您为 TextChanged 事件使用了错误的文本框。您的文本框 8 和 10 正在更改,但您已将显示的代码附加到文本框 7。
public MyForm : Form {
public MyForm(){
InitializeComponents();
// Here you define what TextBoxes should react on user input
textBox8.TextChanged += TextChanged;
textBox10.TextChanged += TextChanged;
}
private void TextChanged(object sender, EventArgs e)
{
int val1;
int val2;
if (!int.TryParse(textBox8.Text, out val1) || !int.TryParse(textBox10.Text, out val2))
return;
Int32 val3 = val1 * val2;
// Here you define what TextBox should show the multiplication result
textBox7.Text = val3.ToString();
}
}
And finally remove the code inside textBox7_TextChanged(object sender, EventArgs e)
最后把里面的代码去掉 textBox7_TextChanged(object sender, EventArgs e)
回答by Craig
Are you getting any compile or bug problems? You need to give your controls meaningful names to better differentiate these controls. Double check that you are referencing your controls properly. In any case, your code should work. My code below catches a failure to convert to int32 below.
您是否遇到任何编译或错误问题?您需要为控件指定有意义的名称以更好地区分这些控件。仔细检查您是否正确引用了控件。无论如何,您的代码应该可以工作。我下面的代码捕获了以下转换为 int32 的失败。
try
{
textBox7.Text = (Convert.ToInt32(textBox10.Text) * Convert.ToInt32(textBox8.Text)).ToString();
}
catch (System.FormatException)
{ }
回答by user2219334
Change your code to the following:
将您的代码更改为以下内容:
private void textBox7_TextChanged(object sender, EventArgs e) {
private void textBox7_TextChanged(对象发送者,EventArgs e){
Int32 val1 = Convert.ToInt32(textBox10.Text);
Int32 val2 = Convert.ToInt32(textBox8.Text);
Int32 val3 = val1 * val2;
textBox7.Text = Convert.ToString(val3);
}

