输入格式不正确

时间:2016-08-23 12:50:43

标签: c# .net

 protected void BtnCal_click(object sender, EventArgs e)
        {
            {    double result = 0;
                double width;
                double length;
                double radius;
                double breath;

            radius = (Convert.ToDouble(TextRadius.Text));
            length = (double.Parse(TextLength.Text));
            breath = (double.Parse(TextBreath.Text));
            width = (Convert.ToDouble(TextWidth.Text));

            if (DropDownShapes.SelectedValue == "cir")
            {                        double pi = 3.142;
                result = radius * radius * pi;
                TextWidth.Text = "0";
                TextLength.Text = "0";
                TextBreath.Text = "0";
            }

            else if (DropDownShapes.SelectedValue == "tri")   
          {    result = length * breath * 0.5;
                TextWidth.Text = "0";
                TextRadius.Text = "0";
            }

            else if (DropDownShapes.SelectedValue == "rec")
            {  result = length * width;
                TextRadius.Text = "0";
                TextLength.Text = "0";
                TextBreath.Text = "0"
            }
            else
            {
                TextArea.Text = result.ToString();
            }
        }
    }

1 个答案:

答案 0 :(得分:-2)

不确定究竟是什么问题,但也许你有一个问题,即你的一个TextBox中没有双重值。要解决此问题,您可以使用double.tryparse而不是Convert.ToDouble或double.parse

double.TryParse(TextRadius.Text, out radius);
double.TryParse(TextLength.Text, out length);
double.TryParse(TextBreath.Text, out breath);
double.TryParse(TextWidth.Text, out width);

Convert.ToDouble和double.Parse将抛出一个exeption,而double.TryParse将返回false,如果不能解析。

此处也回答: c# Double.TryParse or Convert.ToDouble