Textbox left blank = freeze?
Hi! I'm still a bit of a beginner to C# .Net 3.0 and I need some help. I have a program that takes two textbox inputs and adds them together. However when one is left blank it causes the program to freeze. I used a if(Textbox1.Text == null) condition but it still causes it to freeze. I believe I have to use catch and try but I don't know what the exception would be to catch! Anyone know what to do?
Re: Textbox left blank = freeze?
Post the code where you adding the two listboxes together.
Re: Textbox left blank = freeze?
Its just
int tb1;
int tb2;
int output;
tb1 = System.Int32.Parse(textBox1.Text);
tb2 = System.Int32.Parse(textBox2.Text);
output = tb1 + tb2;
textBox3.Text = output;
Re: Textbox left blank = freeze?
Try these statements
Code:
try
{
//your Code Here
}
catch (FormatException) //Handles if You mistakely input String
{
MessageBox.Show("Error! Enter Correctly");
}
catch (ArgumentOutOfRangeException) /* Handles if You mistakely put value but variable is unable to store or in other words it is out of range. This is useful for arrays index too*/
{
MessageBox.Show("Error! Enter Correctly");
}
catch (NullReferenceException) //Handles Empty textboxes or others.
{
MessageBox.Show("Error! Type Correctly");
}
Re: Textbox left blank = freeze?
This could (should if TextBox is blank) fail with exception, but freeze? What exactly it does?
BTW, the condition is better to write
Code:
if(String.IsNullOrEmpty(Textbox1.Text))
(To be exact, I'm not sure if TextBox.Text can even be null, I think it is allways at least String.Empty (=""))
Re: Textbox left blank = freeze?
It is a good programming practice to use exceptional Handling. It solves not only the problem where input is empty but also take cares of other Possible errors...cheers....
Re: Textbox left blank = freeze?
Bad exception handling is worse than none.
Re: Textbox left blank = freeze?
Please explain what is wrong with the error handling codes posted by me above...:)