I have recently started learning C#. I was making a simple form application to calculate sum of fruits (checked through checkboxes) and quantity entered.
Problem is coming in quantity.
Exception is occuring in bold line when I'm running the program without entering any value textBox1 (quantity). Default value is 0. One thing is understood that exception is occuring due to the fact the compiler is in dilemma when it has to convert a blank value (" ") to an int which is rendering an exception. How to handle it?
Another query. I have restricted entry of values in textBoxes to numerics only by using this-
But this is also blocking backspace to work, del is working though. I want backspace to work. My teacher told me a solution based on e.handled only but it was too complex for me too understand (its been few days since I started learning it).
TIY
Problem is coming in quantity.
Code:
[B]if (Convert.ToInt32(textBox1.Text) > 0)[/B]
{
if (checkBox1.Checked)
{
qua1 = Convert.ToInt32(textBox1.Text);
}
else
{
MessageBox.Show("Please Check the respective items before entering values.");
}
}
else
{
MessageBox.Show("Value of Quantity should be positive");
qua1 = 0;
textBox1.Text = "0";
}
Exception is occuring in bold line when I'm running the program without entering any value textBox1 (quantity). Default value is 0. One thing is understood that exception is occuring due to the fact the compiler is in dilemma when it has to convert a blank value (" ") to an int which is rendering an exception. How to handle it?
Another query. I have restricted entry of values in textBoxes to numerics only by using this-
Code:
private void textBox1_KeyPress_1(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar);
}
But this is also blocking backspace to work, del is working though. I want backspace to work. My teacher told me a solution based on e.handled only but it was too complex for me too understand (its been few days since I started learning it).
TIY