private void textquantity_TextChanged(object sender, EventArgs e)
{
string lowitem = "lowitem";
string highitem = "highitem";
if (Convert.ToInt32(textquantity.Text) <= 5)
texthilow.Text = lowitem;
else
texthilow.Text = highitem;
}
Run Code Online (Sandbox Code Playgroud)
我总是得到一个错误
if (Convert.ToInt32(textquantity.Text) <= 5)
Run Code Online (Sandbox Code Playgroud)
请检查您的文本框是否具有empty不会转换为的值int.
因为你在TextChanged事件中获得了价值所以当你press backspace和no value它存在时,它会抛出exception.
if(!string.IsNullOrEmpty(textquantity.Text))
{
int quantity;
if (int.TryParse(textquantity.Text, out quantity))
texthilow.Text = quantity.ToString();
else
texthilow.Text = quantity.ToString();
}
Run Code Online (Sandbox Code Playgroud)
也可以尝试使用int.TryParse,因为它不会throw exception,如果string不是有效的int.
| 归档时间: |
|
| 查看次数: |
621 次 |
| 最近记录: |