Textbox left blank = freeze?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Textbox left blank = freeze?

  1. #1
    Join Date
    Dec 2009
    Posts
    10

    Question 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?

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,281

    Re: Textbox left blank = freeze?

    Post the code where you adding the two listboxes together.

  3. #3
    Join Date
    Dec 2009
    Posts
    10

    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;

  4. #4
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    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 03:19 AM.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  6. #6
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    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....

  7. #7
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  8. #8
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center