简体   繁体   English

textbox1.text为空时获取错误消息框

[英]Getting error message box when textbox1.text is empty

I have a screenshot of the error message ibb.co/dmKd2o . 我有错误消息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. 基本上是说“输入字符串的格式不正确”,并且该错误消息仅在我清除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() : 一种解决方案是使用TryParse()代替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. 这会将aantalgroep设置为成功解析时所期望的值,并将无效字符串的aantalgroep设置为0(真正的default(double)为0)。

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 或仅调用TryParse而不使用if,因为aantalgroep已被初始化为0

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

will fail if textBox1.Text is not a valid numeric string. 如果textBox1.Text不是有效的数字字符串,将失败。 Either catch the exception or use double.TryParse which will return a bool to tell you if it succeeded or not 捕获异常或使用double.TryParse都会返回布尔值,告诉您是否成功

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()double.TryParse()做了两件事。

double.Parse(string s) double.Parse(字符串s)

This method converts a string to double , and returns the value. 此方法将string转换为double ,并返回值。 If the string does not represent a valid value that can be converted to a double it will throw a FormatException . 如果 string不表示可以转换为double FormatException值的有效值,则将抛出FormatException

So, for example, if your string is something like "abc", or if it's an empty string, it will throw the said exception. 因此,例如,如果您的字符串是“ abc”之类的内容,或者它是一个空字符串,则它将引发上述异常。

double.TryParse(string s, out double result) double.TryParse(string s,out double result)

This attempts to convert a string to double . 这将尝试string转换为double If successfully converted, it returns true , and the result will contain the converted value. 如果成功转换,则返回trueresult将包含转换后的值。 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. 但是,如果未成功,它将不会引发异常,而是将返回false ,并且失败情况下的result值将为零。

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. 当此方法返回时,如果转换成功,则包含与s参数等效的双精度浮点数;如果转换失败,则包含零。 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. 如果s参数为null或String.Empty,不是有效格式的数字,或者表示小于MinValue或大于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 . 现在,看一下您的代码,在我看来,您想查看TextBox的值是否可以转换为double ,如果成功,请对该值进行处理,否则在TextBox显示“ 0”。

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 . 获取文本框的值,然后尝试将其转换为double Use double.TryParse() . 使用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. 您可以使用上述方法的返回值和if语句。
  3. If successful, do what you want with the converted double value. 如果成功,则对转换后的double值执行所需操作。 The aantalgroep variable will contain this value. aantalgroep变量将包含此值。
  4. If unsuccessful, simply print "0" to your TextBox . 如果不成功,只需将“ 0”打印到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"; ?
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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