Click to See Complete Forum and Search --> : Textbox left blank = freeze?
redcodefinal
January 5th, 2010, 05:24 PM
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?
Arjay
January 5th, 2010, 08:32 PM
Post the code where you adding the two listboxes together.
redcodefinal
January 5th, 2010, 09:50 PM
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;
rocky_upadhaya
January 6th, 2010, 03:17 AM
Try these statements
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");
}
boudino
January 6th, 2010, 03:22 AM
This could (should if TextBox is blank) fail with exception, but freeze? What exactly it does?
BTW, the condition is better to write
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 (=""))
rocky_upadhaya
January 6th, 2010, 03:38 AM
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....
boudino
January 6th, 2010, 05:58 AM
Bad exception handling is worse than none.
rocky_upadhaya
January 6th, 2010, 06:20 AM
Please explain what is wrong with the error handling codes posted by me above...:)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.