formatexception未处理(double.parse)

时间:2014-12-14 07:24:03

标签: c#

我的申请遇到了一些问题。当我在消息框中单击确定或取消时。它发生了。这是我的代码。它说的字符串不是写的。它说输入字符串不是合成的。我该怎么办。 This is a screenshot

private void button1_Click(object sender, EventArgs e)
    {
       if ((textBox1.Text == "") || (textBox2.Text == "") || (textBox3.Text == "") || (textBox4.Text == ""))
        {
            MessageBox.Show("Please Fill All The Informations", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
        };



        double foot, inches;
        double height;
        double weight;
        double age;
        double BMRM;
        double BMRF;
        double BMI;

        foot = double.Parse(textBox2.Text); 
        inches = double.Parse(textBox1.Text);
        height = 30.48 * (foot + (0.083 * inches));
        weight = double.Parse(textBox3.Text);
        age = double.Parse(textBox4.Text);
        BMRM = 66 + (13.7 * weight) + (5 * height) - (6.8 * age);
        BMRF = 655 + (9.6 * weight) + (1.8 * height) - (4.7 * age);
        BMI = (weight / ((height * height) / 10000));

        if (radioButton1.Checked)
        {
            textBox5.Text = BMRM.ToString();
        };
        if (radioButton2.Checked)
        {
            textBox5.Text = BMRF.ToString();
        };
        textBox6.Text = BMI.ToString();

        if (BMI <= 18.5)
        {
            textBox7.Text = "Your weight is low. You have to eat properly.";
        };
        if (BMI > 18.5 && BMI <= 24.9)
        {
            textBox7.Text = "You are Healthy";
        };
        if (BMI > 25 && BMI <= 29.9)
        {
            textBox7.Text = "You have gained a little fat";
        };

        if (BMI > 30 && BMI <= 34.9)
        {
            textBox7.Text = "You are in the fist step of Fat.You have take exercise regularly";
        };
        if (BMI > 35 && BMI < 40)
        {
            textBox7.Text = "You are in the second step of Fat. You need to exercise regularly";
        }
        if (BMI > 40)
        {
            textBox7.Text = "You have gained a lot of fat. It can cause you death. Please take Doctor's advise";
        };

1 个答案:

答案 0 :(得分:0)

您可以尝试这样:

double foot;
if(!double.TryParse(textBox2.Text,out foot))
  MessageBox.Show(String.Format("Invalid input, foot must be a number"));