-
January 5th, 2010, 06:24 PM
#1
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?
-
January 5th, 2010, 09:32 PM
#2
Re: Textbox left blank = freeze?
Post the code where you adding the two listboxes together.
-
January 5th, 2010, 10:50 PM
#3
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;
-
January 6th, 2010, 04:17 AM
#4
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");
}
Last edited by rocky_upadhaya; January 6th, 2010 at 04:19 AM.
-
January 6th, 2010, 04:22 AM
#5
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 (=""))
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
January 6th, 2010, 04:38 AM
#6
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....
-
January 6th, 2010, 06:58 AM
#7
Re: Textbox left blank = freeze?
Bad exception handling is worse than none.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
January 6th, 2010, 07:20 AM
#8
Re: Textbox left blank = freeze?
Please explain what is wrong with the error handling codes posted by me above...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|