C# 输入字符串的格式不正确#
C# 输入字符串的格式不正确#,c#,C#,private void textquantity\u TextChanged(对象发送方,事件参数e) { 字符串lowitem=“lowitem”; 字符串highitem=“highitem”; 如果(Convert.ToInt32(textquantity.Text)请检查您的文本框是否有空值它将不会转换为int 由于您在TextChanged事件中获取值,因此当您按backspace时,如果中没有值,它将抛出异常 if(!string.IsNullOrEmpty(textquantit
private void textquantity\u TextChanged(对象发送方,事件参数e)
{
字符串lowitem=“lowitem”;
字符串highitem=“highitem”;
如果(Convert.ToInt32(textquantity.Text)请检查您的文本框是否有空值
它将不会转换为int
由于您在TextChanged
事件中获取值,因此当您按backspace
时,如果中没有值,它将抛出异常
if(!string.IsNullOrEmpty(textquantity.Text))
{
int quantity;
if (int.TryParse(textquantity.Text, out quantity))
texthilow.Text = quantity.ToString();
else
texthilow.Text = quantity.ToString();
}
还应尝试使用int.TryParse
,因为如果string
不是有效的int
,请尝试使用TryParse
而不是Parse
:
int value;
if (int.TryParse(textquantity.Text, out value))
if (value <= 5)
texthilow.Text = lowitem;
else
texthilow.Text = highitem;
else {
// textquantity.Text contains an invalid value, e.g. "bla-bla-bla"
}
int值;
if(int.TryParse(textquantity.Text,out值))
如果(value More info.Runtime error or or compile?实际错误是什么?textquantity.Text
的值是什么?您的CurrentCulture
是什么?请阅读几遍..从图像中可以清楚地看出,文本是空的。使用int.TryParse
或检查是否有空stirng
if(!string.IsNullOrEmpty(textquantity.Text))
{
int quantity;
if (int.TryParse(textquantity.Text, out quantity))
texthilow.Text = quantity.ToString();
else
texthilow.Text = quantity.ToString();
}
int value;
if (int.TryParse(textquantity.Text, out value))
if (value <= 5)
texthilow.Text = lowitem;
else
texthilow.Text = highitem;
else {
// textquantity.Text contains an invalid value, e.g. "bla-bla-bla"
}