Share via


C#: Body Mass Index (BMI) Calculator

-

Preface

In this chapter we want  to Calculate Body Mass Index (BMI).

Process

The following steps show how Calculate Body Mass Index (BMI):

  1. First, Click New Project in Start Page or On File Menu.
  2. In New Project Dialog , Click Windows On Left Pane And Windows Forms Application On Middle Pane.
  3. Change form layout to this Mode:

https://code.msdn.microsoft.com/site/view/file/170349/1/FormLayout.jpg

4.Add these Items To ComboBox :

  • 19 to 24
  • 25 to 34
  • 35 to 44
  • 45 to 54
  • 55 to 64
  • More Than 65

5.Set BMI Textbox Enable To False.

6.We Know The BMI  Is Mass(Kg) / (height(m))2.Then Into Click Event Of Calculate Your BMI Button Write This Code :

double bmi;
 double vazn = double.Parse(numericUpDown2.Value.ToString());
 double ghad2 = Math.Pow(double.Parse(numericUpDown1.Value.ToString()), 2);
 bmi = vazn / ghad2;
 if (bmi.ToString().Length > 5)
 {
 textBox1.Text = (vazn / ghad2).ToString().Remove(5);
 }
 else
 {
 textBox1.Text = (vazn / ghad2).ToString();
 }
 if (bmi < 16.5 && bmi > 11)
 {
 label15.Visible = true;
 label16.Visible = false;
 label17.Visible = false;
 label18.Visible = false;
 label19.Visible = false;
 }
 
 else if  (bmi < 18.5 && bmi > 16.5)
 {
  label15.Visible = false;
  label16.Visible = true;
  label17.Visible = false;
  label18.Visible = false;
  label19.Visible = false;
  }
  else if  (bmi < 25 && bmi > 18.5)
  {
  label15.Visible = false;
  label16.Visible = false;
  label17.Visible = true;
  label18.Visible = false;
  label19.Visible = false;
  }
  else if  (bmi < 30 && bmi > 25)
  {
  label15.Visible = false;
  label16.Visible = false;
  label17.Visible = false;
  label18.Visible = true;
  label19.Visible = false;
  }
  else if  (bmi > 30)
  {
  label15.Visible = false;
  label16.Visible = false;
  label17.Visible = false;
  label18.Visible = true;
  label19.Visible = false;
  }

7.For Show Appropriate BMI , Into The SelectedIndexChanged Event Of Age Range ComboBox , Write This Code :

if (comboBox1.SelectedIndex == 0)
 {
 label14.Text = "22";
 }
 else if  (comboBox1.SelectedIndex == 1)
 {
 label14.Text = "23";
 }
 else if  (comboBox1.SelectedIndex == 2)
 {
 label14.Text = "24";
 }
 else if  (comboBox1.SelectedIndex == 3)
 {
 label14.Text = "25";
 }
 else if  (comboBox1.SelectedIndex == 4)
 {
 label14.Text = "26";
 }
 else if  (comboBox1.SelectedIndex == 5)
 {
 label14.Text = "27";
 }