C#中的简单输入验证

时间:2017-01-31 03:03:54

标签: c#

我正在尝试将输入验证添加到文本框中,以确保如果用户输入除我的标签之外的任何其他内容,则会说"仅输入一个数字。"我尝试过使用' if'这个声明(看下面的代码),但它没有用。而不是将消息发布到"输入一个数字"我的程序崩溃时出现此错误消息

  

' System.FormatException':输入字符串的格式不正确

我是菜鸟,所以我不确定如何解决这个问题。

命名空间计算器 {     public partial class frmCalculator:Form     {         public frmCalculator()         {             的InitializeComponent();         }

    string name = "";//Stores the Name typed into the text box
    string classType = "";//Stores the Class typed into the text box
    decimal grade;// Stores the Grade typed into the text box


    private void btnSubmit_Click(object sender, EventArgs e)
    {

        classType = txtBoxClass.Text;
        grade = Convert.ToDecimal(txtBoxGrade.Text);
        name = txtBoxName.Text;

        switch (classType)
        {
            //Case for Math class and grade comparison
            case "m":
            case "M":
                if (grade >= -1 && grade <= 100)
                {
                    if (grade >= 94)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an A";
                    }
                    if (grade <= 93)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an A-";
                    }
                    if (grade <= 89)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a B+";
                    }
                    if (grade <= 86)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an B";
                    }
                    if (grade <= 83)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a B-";
                    }
                    if (grade <= 79)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a C+";
                    }
                    if (grade <= 76)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a C";
                    }
                    if (grade <= 73)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a C-";
                    }
                    if (grade <= 69)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a D";
                    }
                    if (grade < 65)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an F";
                    }

                    //Clears the text boxes when the Submit button is clicked.
                    txtBoxName.Text = "";
                    txtBoxClass.Text = "";
                    txtBoxGrade.Text = "";
                }
                else { lblAnswer.Text = "Input a number!"; }
                break;

            //Case for Science class and grade comparison
            case "s":
            case "S":
                if (grade >= 94)
                {
                    lblAnswer.Text = name + "'s grade in Science is an A";
                }
                if (grade <= 93)
                {
                    lblAnswer.Text = name + "'s grade in Science is an A-";
                }
                if (grade <= 89)
                {
                    lblAnswer.Text = name + "'s grade in Science is a B+";
                }
                if (grade <= 86)
                {
                    lblAnswer.Text = name + "'s grade in Science is an B";
                }
                if (grade <= 83)
                {
                    lblAnswer.Text = name + "'s grade in Science is a B-";
                }
                if (grade <= 79)
                {
                    lblAnswer.Text = name + "'s grade in Science is a C+";
                }
                if (grade <= 76)
                {
                    lblAnswer.Text = name + "'s grade in Science is a C";
                }
                if (grade <= 73)
                {
                    lblAnswer.Text = name + "'s grade in Science is a C-";
                }
                if (grade <= 69)
                {
                    lblAnswer.Text = name + "'s grade in Science is a D";
                }
                if (grade < 65)
                {
                    lblAnswer.Text = "'s grade in Science is an F";
                }

                //Clears the text boxes when the Submit button is clicked.
                txtBoxName.Text = "";
                txtBoxClass.Text = "";
                txtBoxGrade.Text = "";

                break;

            //Case for English class and grade comparison.
            case "e":
            case "E":
                if (grade >= 94)
                {
                    lblAnswer.Text = name + "'s grade in English is an A";
                }
                if (grade <= 93)
                {
                    lblAnswer.Text = name + "'s grade in English is an A-";
                }
                if (grade <= 89)
                {
                    lblAnswer.Text = name + "'s grade in English is a B+";
                }
                if (grade <= 86)
                {
                    lblAnswer.Text = name + "'s grade in English is an B";
                }
                if (grade <= 83)
                {
                    lblAnswer.Text = name + "'s grade in English is a B-";
                }
                if (grade <= 79)
                {
                    lblAnswer.Text = name + "'s grade in English is a C+";
                }
                if (grade <= 76)
                {
                    lblAnswer.Text = name + "'s grade in English is a C";
                }
                if (grade <= 73)
                {
                    lblAnswer.Text = name + "'s grade in English is a C-";
                }
                if (grade <= 69)
                {
                    lblAnswer.Text = name + "'s grade in English is a D";
                }
                if (grade < 65)
                {
                    lblAnswer.Text = name + "'s grade in English is an F";
                }

                //Clears the text boxes when the Submit button is clicked.
                txtBoxName.Text = "";
                txtBoxClass.Text = "";
                txtBoxGrade.Text = "";

                break;

            default:
                lblAnswer.Text = "Invalid Class type! Enter M for Math, S for Science or \nE for English only.";
                txtBoxClass.Text = "";//Clears just the Class text box.

                break;
    }

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        //Exits the application when the Exit button is clicked.
        this.Close();
    }

}

}

4 个答案:

答案 0 :(得分:6)

您应该使用数字基元的TryParse方法来查看是否可以将输入转换为数字。

decimal grade;
if (Decimal.TryParse(txtBoxGrade.Text, out grade)) {
    // Its a valid number - the rest of your code goes here
    // and uses the grade variable as the number you want.
} else {
    // Its not a valid number
    lblAnswer.Text = "Input a number only.";
}

答案 1 :(得分:1)

使用Decimal.TryParse检查输入文本是否为小数而不抛出异常(如果不是)。

if(Decimal.TryParse(txtBoxGrade.Text, out grade))
{
   // Your code
}
else
{
   lblAnswer.Text = "Input a number!";
}

答案 2 :(得分:0)

您可以使用TryParse查看它是否为数字。

MSDN Decimal.TryParse

decimal grade;
if(!decimal.TryParse(txtBoxGrade.Text, out grade))
{
     MessageBox.Show(string.Format("Unable to parse '{0}'.", txtBoxGrade.Text));
     return;
}

答案 3 :(得分:0)

请尝试以下操作。您可以使下面的代码更好。无论如何,保持改进尽可能接近你的代码,你可以做出几点改进: -

  1. 使用TryParse检查成绩是否为有效小数。
  2. 使用if if。
  3. 条件if(等级> -1&amp;&amp; grade&lt; = 100),现在检查0到100之间的小数。之前它也允许-1。根据您的要求,请相应地更改条件。
  4. 以下代码可以为您提供帮助。

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        var classType = txtBoxClass.Text;
        var name = txtBoxName.Text;
    
        decimal grade;
        if (! decimal.TryParse(txtBoxGrade.Text, out grade))
        {
            lblAnswer.Text = "Input a number!";
            return;
        }            
    
        switch (classType)
        {
            //Case for Math class and grade comparison
            case "m":
            case "M":
                if (grade > -1 && grade <= 100)
                {
                    if (grade >= 94)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an A";
                    }
                    else if (grade <= 93)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an A-";
                    }
                    else if (grade <= 89)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a B+";
                    }
                    else if (grade <= 86)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an B";
                    }
                    else if (grade <= 83)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a B-";
                    }
                    else if (grade <= 79)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a C+";
                    }
                    else if (grade <= 76)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a C";
                    }
                    else if (grade <= 73)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a C-";
                    }
                    else if (grade <= 69)
                    {
                        lblAnswer.Text = name + "'s grade in Math is a D";
                    }
                    else if (grade < 65)
                    {
                        lblAnswer.Text = name + "'s grade in Math is an F";
                    }
    
                    //Clears the text boxes when the Submit button is clicked.
                    txtBoxName.Text = "";
                    txtBoxClass.Text = "";
                    txtBoxGrade.Text = "";
                }
                else { lblAnswer.Text = "Input a number between 0 and 100!"; }
                break;
        }
    }