示例#1
0
        /// <summary>
        /// Calculates the BMI value, writes the result in BMITextBox.Text and chebge the color of the BMI Scale accordingly
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CalculateBMIButton_Click(object sender, EventArgs e)
        {
            // Holds the calculated BMI value
            double BMI;

            // Makes sure BMITextBox.Font size is 20
            BMITextBox.Font = new Font("Microsoft Sans Serif", 20);
            try
            {
                if (double.Parse(WeightTextBox.Text) == 0 || double.Parse(HeightTextBox.Text) == 0)
                {
                    throw new FormatException();
                }
                // BMI value calculation
                BMI = double.Parse(WeightTextBox.Text) / Math.Pow(double.Parse(HeightTextBox.Text), 2.0);
                // Adjusts the calculated BMI value if Imperial units are chosen
                if (ImperialRadioButton.Checked)
                {
                    BMI *= 703;
                }
                // Resets the color of each label in BMIScaleTableLayoutPanel to system default
                ResetScaleColors();
                // Changes the BackColor of the specific label in BMIScaleTableLayoutPanel according to the calculated BMI value
                if (BMI < 18.5)
                {
                    UnderweightLabel1.BackColor = Color.LightCoral;
                    UnderweightLabel2.BackColor = Color.LightCoral;
                }
                else if (BMI >= 18.5 && BMI <= 24.9)
                {
                    NormalLabel1.BackColor = Color.LawnGreen;
                    NormalLabel2.BackColor = Color.LawnGreen;
                }
                else if (BMI >= 25 && BMI <= 29.9)
                {
                    OverweightLabel1.BackColor = Color.LightCoral;
                    OverweightLabel2.BackColor = Color.LightCoral;
                }
                else if (BMI >= 30)
                {
                    ObeseLabel1.BackColor = Color.LightCoral;
                    ObeseLabel2.BackColor = Color.LightCoral;
                }
                // Writes the calculated BMI value to BMITextBox.Text formated with 2 decimal places
                BMITextBox.Text = BMI.ToString("f1");
                // Hides ButtonsTableLayoutPanel
                ButtonsTableLayoutPanel.Visible = false;
                // Shows BMIScaleTableLayoutPanel
                BMIScaleTableLayoutPanel.Visible = true;
            }// Handles FormatException when WeightTextBox.Text or HeightTextBox.Text is empty string and the function tries to parse it
            catch (FormatException)
            {
                BMITextBox.Font = new Font("Microsoft Sans Serif", 11);
                BMITextBox.Text = "Invalid values";
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#2
0
 /// <summary>
 /// This event displays the BMI result and the scale after calculations.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Calculate_Click(object sender, EventArgs e)
 {
     if (Imperial.Checked == true)
     {
         double height = double.Parse(HeightText.Text);
         double Weight = double.Parse(WeightText.Text);
         double BMI;
         BMI             = (Weight * 703) / (height * height);
         BMIResults.Text = BMI.ToString();
         if (BMI <= 18.5)
         {
             result.Text = "Underweight";
         }
         else if (BMI > 18.5 && BMI <= 24.9)
         {
             result.Text = "Normal";
         }
         else if (BMI > 25 && BMI <= 29.9)
         {
             result.Text = "Overweight";
         }
         else if (BMI >= 30)
         {
             result.Text = "Obese";
         }
     }
     if (Metric.Checked == true)
     {
         double height = double.Parse(HeightText.Text);
         double Weight = double.Parse(WeightText.Text);
         double BMI;
         BMI             = (Weight) / (height * height);
         BMIResults.Text = BMI.ToString();
         if (BMI <= 18.5)
         {
             result.Text = "Underweight";
         }
         else if (BMI > 18.5 && BMI <= 24.9)
         {
             result.Text = "Normal";
         }
         else if (BMI > 25 && BMI <= 29.9)
         {
             result.Text = "Overweight";
         }
         else if (BMI >= 30)
         {
             result.Text = "Obese";
         }
     }
 }