如果我从一个正数中减去一个正数,代码将起作用。
例如,4-1=3
。 问题是当我减去一个负数,如3 - -2
。
这应该等于5
。
当我输入 ' - ' 在文本框txt_p7(第二个文本框), 它示出了在这个代码中的错误:
txt_result.Text = (Convert.ToInt32(txt_p1.Text) - Convert.ToInt32(txt_p7.Text)).ToString();
这里是我正在使用的代码:
private void txt_p1_TextChanged(object sender, EventArgs e)
{ if ((!string.IsNullOrEmpty(txt_p1.Text)) && (!string.IsNullOrEmpty(txt_p7.Text)))
txt_result.Text = (Convert.ToInt32(txt_p1.Text) - Convert.ToInt32(txt_p7.Text)).ToString();
}
private void txt_p7_TextChanged(object sender, EventArgs e)
{
if ((!string.IsNullOrEmpty(txt_p1.Text)) && (!string.IsNullOrEmpty(txt_p7.Text)))
txt_result.Text = (Convert.ToInt32(txt_p1.Text) - Convert.ToInt32(txt_p7.Text)).ToString();
}
什么是错误讯息? –
纠正错误的第一步是*读取错误消息*。 – David
FormatException是未处理的.. 我第一次键入的值,no1 = -3,no2 = 4,结果显示= -7 .. 那好吧,之后,我改变了值no1或no2,有错误.. –