windows-phone-7 – 例外; mscorlib.ni.dll中出现“System.FormatException”类型的异常,但未在用户代码中处理

前端之家收集整理的这篇文章主要介绍了windows-phone-7 – 例外; mscorlib.ni.dll中出现“System.FormatException”类型的异常,但未在用户代码中处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你好我有一个 Windows Phone 8应用程序,我得到了例外

An exception of type ‘System.FormatException’ occurred in mscorlib.ni.dll but was not handled in user code

Here are the code

private void Button_Click_1(object sender,RoutedEventArgs e)
    {
        double basestolen;
        double attempedstales;
        double avarege;
        double putout;




         if (puttext.Text.Length == 0 | basetext.Text.Length==0 )
        {

            MessageBox.Show(" Enter Values for Base Stolen and Putouts ");

        }

         basestolen = Convert.ToDouble(basetext.Text);
        putout = Convert.ToDouble(puttext.Text);



        attempedstales = basestolen + putout;


        if (attempedstales != 0  )
        {

            avarege = (((basestolen / attempedstales) / 100));
            avarege = avarege * 10000;
            avgtext.Text = Convert.ToString(avarege);


        }
        else
        {
            MessageBox.Show("Attemped Stales Value should not be Zero");
        }



    }

应用程序运行,如果我没有在文本框中输入值,它将返回msg框,但之后应用程序停止并返回上面的exption?问题是什么?

解决方法

错误很可能在这里:

basestolen = Convert.ToDouble(basetext.Text);
putout = Convert.ToDouble(puttext.Text);

如果数字不是有效格式,则抛出FormatException. (see more here).尝试使用double.TryParse以安全的方式解析您的值.

double result;    
bool success = double.TryParse(basetext.Text,NumberStyles.Any,CultureInfo.InvariantCulture,out result);

猜你在找的Windows相关文章