输入字符串格式不正确C#

Bio*_*bio 0 c#

在此输入图像描述

   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)

Mai*_*mad 6

请检查您的文本框是否具有empty不会转换为的值int.

因为你在TextChanged事件中获得了价值所以当你press backspaceno 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.