简体   繁体   English

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

[英]Input String was not in correct format in C#

Not sure how to fix this.不知道如何解决这个问题。 I am a noob to C#.我是 C# 的菜鸟。 enter link description here在此处输入链接描述

Your TextBox contains not a valid integer, try this code您的 TextBox 包含无效的整数,试试这个代码

public void txtbox1_TextChanged(object sender, EventArgs e)
{
    int number;
    if (!Int32.TryParse(txtbox1.Text, out number))
    {
        MessageBox.Show("Number is invalid");
    }

    if (number == 2112)
    {
        this.BackColor = Color.Blue;
        return;
    }

    this.BackColor = Color.HotPink;
}

Wherever user input is concerned use validation (either in the code behind or on the textbox, ideally both).无论用户输入在哪里,都使用验证(在后面的代码中或在文本框上,理想情况下两者)。 Most likely your textbox does not contain an integer value.很可能您的文本框不包含整数值。 Try this:尝试这个:

int number;
if(Int32.TryParse(textbox1.Text, out number))
{
    // Logic on validated input
} 
else  
{
    // Error message
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM