【发布时间】:2017-11-20 16:13:17
【问题描述】:
我在运行时更改Textbox 的值,但是当用户使用退格键清除Textbox 时,我得到了无效字符串的异常。
private void txtTradePrice_TextChanged(object sender, EventArgs e)
{
txtRate.Text = (Convert.ToInt32(txtTradePrice.Text) * 12).ToString();
}
private void txtRate_TextChanged(object sender, EventArgs e)
{
txtTradePrice.Text = (Convert.ToInt32(txtRate.Text) / 12).ToString();
}
【问题讨论】:
-
因为空字符串不能转换为整数。看看使用 Int.TryParse() 代替。
-
Google 获取通用错误文本。
-
您得到的字符串无效,因为您无法将空字符串转换为 int。此外,据我所知,在属性上设置
.Text字段将触发TextChanged事件,因此您将获得循环触发。