I have simple calculator in C#.NET WF. But when I insert to my editBox something else than number, my program crash. How should I do to avoid this crash?
private void doMath()
{
int n1 = Int32.Parse(editBox1.Text);
int n2 = Int32.Parse(editBox2.Text);
String o = operation.Text;
switch(o)
{
case "+": result.Text = (n1 + n2).ToString();
break;
case "-": result.Text = (n1 - n2).ToString();
break;
case "*": result.Text = (n1 * n2).ToString();
break;
case "/": result.Text = (n1 / n2).ToString();
break;
case "%": result.Text = (n1 % n2).ToString();
break;
default: result.Text = "No operator selected.";
break;
}
}//end doMath
private void editBox1_TextChanged(object sender, EventArgs e)
{
doMath();
}
int.TryParse
.