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();
}
}
}
FormatExceptionclassType = txtBoxClass.Text; **grade = Convert.ToDecimal(txtBoxGrade.Text);** this is what throws the error name = txtBoxName.Text;txtBoxGrade.Textcan't be converted to a decimal. What value does it contain in your watch window?name,classtypeandgrade?