0

So i'm making a self grading aptitude test that is done except for, if I backspace the entire field of a textbox to be empty is errors out and closes, not upon pressing but upon clearing all the characters. I've been reading some documentation and have had a hard time finding a proper way to implement some way to handle this error. the message after crashing is: This exception was originally thrown at this call stack:

private void AA3_TextChanged(object sender, EventArgs e)
        {
            int A3 = Int32.Parse(AA3.Text);
            if (A3 == 14200)
            {

                percent +=1;
                exp.Text = percent.ToString();

            }
        }

the code inside is just a counter for the grade at the end, but i am lost why some backspace is okay but not a clear field. Sorry if this is poorly worded.

Clambert
  • 33
  • 7
  • 1
    What integer value do you think an empty string parses to? – Ňɏssa Pøngjǣrdenlarp Jun 02 '20 at 17:36
  • What happens if that AA3.Text is empty or not a number? Int32.Parse cannot handle this context and throws an exception. Int32.TryParse can handle it. – Steve Jun 02 '20 at 17:36
  • You're hitting this problem because you're not checking for null. Check the value of the textBox.Text property before you do any calculations: if(string.IsNullOrWhiteSpace(textBox1.Text)) { return; } – LordPupazz Jun 02 '20 at 17:37
  • @LordPupazz `AA3.Text` cannot be null. It can be empty, which is the issue. –  Jun 02 '20 at 17:38
  • @Amy - Good point, but same difference, the program still crashes :) – LordPupazz Jun 02 '20 at 17:40
  • IsNullOrEmpty/IsNullOrWhiteSpace will not save the day if the user input is something like 'Steve' – Steve Jun 02 '20 at 17:44
  • @Steve Sorry if this is a dumb question but would would my out variable be for TryParse? – Clambert Jun 02 '20 at 19:57
  • You should have a look at [this technique](https://www.c-sharpcorner.com/blogs/how-to-use-validation-in-windows-form-application) of using the built-in validation for text fields. But instead of where they have `string.IsNullOrWhiteSpace(textBoxName.Text)` use `int.TryParse(AA3.Text, out int A3)` – Wyck Jun 02 '20 at 19:58
  • There is a good answer below from @Wyck that shows you how to do your checks. – Steve Jun 02 '20 at 20:11

2 Answers2

2

Here's a minimal fix for you.

private void AA3_TextChanged(object sender, EventArgs e)
{
    if (int.TryParse(AA3.Text, out int A3)) {
        if (A3 == 14200)
        {
            percent +=1;
            exp.Text = percent.ToString();
        }
    } else {
        // The input is not valid.
        // TODO: Consider telling the user nicely about that.
    }
}
Wyck
  • 10,311
  • 6
  • 39
  • 60
  • I would not add a message for the else part. Consider that TextChanged is called for each keystroke. I would go for something less invasive like a change in background color or some other visual effect to highlight the invalid value like [this one](https://stackoverflow.com/questions/8716917/how-to-show-a-net-balloon-tooltip) for example. – Steve Jun 02 '20 at 20:16
  • @Steve, good point. Something like MessageBox.Show would be the absolute worst experience, wouldn't it? Tooltips and background colours a bit more effective, but a little error icon with some supporting red error message text within the form positioned just below the offending field, which can be outlined in red, is definitely the way to go! Search the web for _form validation user experience_. [example](https://www.nngroup.com/articles/errors-forms-design-guidelines/) – Wyck Jun 03 '20 at 00:44
1

You have to either use Int32.TryParse or validate the text before parsing.

if(string.IsNullOrEmpty("string")) return;

Or

Int32.TryParse("331", out int value);
fared
  • 161
  • 2
  • 12
  • IsNullOrEmpty/IsNullOrWhiteSpace will not save the day if the user input is something like 'Steve' – Steve Jun 02 '20 at 17:43
  • Why not? the problem is the value pass to the event TextChanged is null, empty and trying to convert that to an Int32 type, thus the exception. Or am I missing something? – fared Jun 02 '20 at 17:46
  • 1
    Because IsNullOrEmpty will return false if I start typing with the letter "S" in the text box. TextChanged is called in that moment and is useless checking with IsNullOrEmpty/WhiteSpace and then go on with Int32.Parse. At this point just use Int32.TryParse and continue only if it returns true. – Steve Jun 02 '20 at 17:50