输入格式错误

本文关键字:错误 格式 输入 | 更新日期: 2023-09-27 18:03:31

 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();
            }
        }
    }

输入格式错误

不确定到底是什么问题,但也许你有问题,没有双值在你的一个文本框。为了解决这个问题,你可以用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);

转换。加倍,加倍。Parse将在double时抛出异常。如果无法解析,TryParse将返回false。

也回答在这里:c#翻倍。尝试解析或转换。ToDouble