简体   繁体   中英

Getting error message box when textbox1.text is empty

I have a screenshot of the error message ibb.co/dmKd2o . It's basically saying "the format of the input string is incorrect", and that error message only comes up everytime i clear textbox1.text.

Here comes the code:

/*This is located inside public partial class Form1 : Form*/
        double aantalgroep = 0;
        double number = 0;

 /*This is located inside private void Calculate()*/       

        aantalgroep = double.Parse(textBox1.Text);

        /* Wat er gebeurd bij RadioButton1 Checked */
        if (radioButton1.Checked)
        {
            number = aantalgroep * 8;

            textBox2.Text = number.ToString();

      /* I tried this but this doesn't work? */
            if (textBox1.Text == "")
            { aantalgroep = 0;
            } else
            {
                aantalgroep = double.Parse(textBox1.Text);
            }
/* From here everything is oke( i think ) */


            if (aantalgroep < 10)
            {
                textBox2.Text = number.ToString();
            }

}

One solution would be to use TryParse() instead of Parse() :

double.TryParse(textBox1.Text, out aantalgroep);

This will set aantalgroep to the value you're expecting on a successful parse, and set aantalgroep to 0 (really default(double) which is 0) for an invalid string.

In this line you get that error when the textbox is empty

aantalgroep = double.Parse(textBox1.Text);

You need to change it with

if(!double.TryParse(textBox1.Text, out aantalgroep))
   aantalgroep = 0;

or just call TryParse without the if because the aantalgroep is alread initialized with 0

double.TryParse(textBox1.Text, out aantalgroep);
aantalgroep = double.Parse(textBox1.Text);

will fail if textBox1.Text is not a valid numeric string. Either catch the exception or use double.TryParse which will return a bool to tell you if it succeeded or not

use this code:

if (string.IsNullOrEmpty(textBox1.Text))
        { aantalgroep = 0;
        }

I think I mentioned this in a comment in a different question as well.

The two methods double.Parse() and double.TryParse() do two slightly different things.

double.Parse(string s)

This method converts a string to double , and returns the value. If the string does not represent a valid value that can be converted to a double it will throw a FormatException .

So, for example, if your string is something like "abc", or if it's an empty string, it will throw the said exception.

double.TryParse(string s, out double result)

This attempts to convert a string to double . If successfully converted, it returns true , and the result will contain the converted value. However, if unsuccessful, it will NOT throw an exception, but will return false instead, and the value of result in a failure situation will be zero.

See below:

When this method returns, contains the double-precision floating-point number equivalent of the s parameter, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.

Now, looking at your code, it seems to me that you want to see if the value of a TextBox can be converted to a double , and if successful, do something with the value and else, display a "0" into the TextBox .

So, try to formulate in your mind what you need to do. It will be something like this:

  1. Get the value of text box and try to convert it to a double . Use double.TryParse() .
  2. See if the above conversion succeeded or not. You can use the return value of the above method and an if statement for this.
  3. If successful, do what you want with the converted double value. The aantalgroep variable will contain this value.
  4. If unsuccessful, simply print "0" to your TextBox .

It should look something like this:

double aantalgroep = 0;
double number = 0;

if (radioButton1.Checked)
{
    if (double.TryParse(textBox1.Text, out aantalgroep))
    {
        // Stuff you do when successful.
        number = aantalgroep * 8;
        textBox2.Text = number.ToString();
    }
    else
    {
        // Stuff you do when unsuccessful.
        // Something like textBox2.Text = "0"; ?
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM