简体   繁体   中英

formatexception was unhandled (double.parse)

I'm having some problem with my application. when i click ok or cancel in the messagebox. it happens. Here is my code. it says that the string of foot is not write. It says input string is not in formate. What should i do. 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";
        };

You can try like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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