2

I am trying to add input validation to a text box to make sure that if the user enter anything other than a number one of my labels will say "Input a number only." I have tried using an 'if' statement for this (look at code below) but it did not work. Instead of posting the message to "input a number" my program crashes with this error message

'System.FormatException': Input string was not in a correct format

I am a noob so I'm not sure how to fix this.

namespace Calculator { 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();
    }

}

}

8
  • 1
    Post more of your code. Be sure to include the line that is throwing the exception. I don't see anything in what you've posted that would throw a FormatException Commented Jan 31, 2017 at 3:08
  • @Amy Sorry, I posted the line that threw the error but it seems like it got cut off. Here's what's above the start of the 'Switch" classType = txtBoxClass.Text; **grade = Convert.ToDecimal(txtBoxGrade.Text);** this is what throws the error name = txtBoxName.Text; Commented Jan 31, 2017 at 3:12
  • 1
    Based on your comment, I'd say txtBoxGrade.Text can't be converted to a decimal. What value does it contain in your watch window? Commented Jan 31, 2017 at 3:18
  • what type is name, classtype and grade? Commented Jan 31, 2017 at 3:21
  • @reds Yes. The only variable that I declared as a decimal is 'Grade' Commented Jan 31, 2017 at 3:21

4 Answers 4

6

You should use the TryParse methods of the numerical primitives to see if converting input to a number is possible.

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.";
}
Sign up to request clarification or add additional context in comments.

1 Comment

This fixed it. Thank you so much.
1

Use Decimal.TryParse to check whether the input text is a decimal without throwing an exception if it is not.

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

Comments

0

You can see if it is a number by using TryParse.

MSDN Decimal.TryParse

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

Comments

0

Try below. You can make the below code more better. Anyway, keeping improvement as close to your code, there are couple of improvement you can make:-

  1. Use of TryParse to check if grade is a valid decimal or not.
  2. Use else if.
  3. The condition if (grade > -1 && grade <= 100), now checks for decimal between 0 and 100. Earlier it was allowing -1 too. It that's your requirement then please change the condition accordingly.

Below code will assist you.

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.