Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple Metrics System BMI Calculator in Windows Forms

0.00/5 (No votes)
29 May 2010 1  
A simple metrics system BMI calculator in Windows Forms

Introduction

Designing apps in Microsoft Visual C++ 2005 Express Edition is pretty straight forward. Point this, click that, presto! Place it on form. But seriously, click on the desired component (control), located at the left toolbar of Visual C++ 2005 Express Edition, and draw it at an appropriate location on the form. Components on the form align to rectangular grids, giving your apps a symmetric look.

This simple app demonstrates the ease of creating a simple metrics system BMI calculator in Windows Forms using C++.

BMI

But first: The body mass index is a statistical measure used to estimate a healthy body weight and is the most widely used diagnostic tool and is utilized to identify whether individuals are underweight, overweight or obese.

BMI Determinants

  • Underweight = <18.5
  • Normal weight = 18.5-24.9
  • Overweight = 25-29.9
  • Obesity = BMI of 30 or greater

DataGridView

Fast forwarding a bit, we've created a Windows Form, placed a horizontal scrollbar, plus some buttons and textboxes. To have the control respond to mouse clicks, we have to put some code into it. So we double-click on a control and we are presented with an event method. All we have to do is place some code in that control's event method.

Let's click on a hScrollBar and place relevant code in its dataGridView1_CellContentClick( ) section, to have it handle some events.

The hScrollBar1_ValueChanged Event

The hScrollBar1_ValueChanged event is called upon when the scrollbar values change when a user interacts with it by dragging it for instance. As this is being done, the application updates the displays by making calls to setDiagnos() and calculateBMI() to calculate and display the new BMI values.

private: System::Void hScrollBar1_ValueChanged
	(System::Object^  sender, System::EventArgs^  e)
{
  	hScrollBar1->Value=height;
	hScrollBar2->Value=Weight;
	hScrollBar3->Value=BMI;
} 
public: void setDiagnos()
{
	double Weight2=BMI*((height/100)*(height/100));
	double bmiDiff=0;
	if (BMI>=22) bmiDiff=BMI-22; if (BMI<22) bmiDiff=22-BMI;
	double preferredWeight=bmiDiff*((height/100)*(height/100));

	if ((BMI>=-1)&& (BMI<7))  Diagnos=" BMI under 7 is rare  ";
	if ((BMI>6) &&  (BMI<11)) Diagnos=" Health hazrd,  Cardiac  stress ";
	if ((BMI>10) && (BMI<16)) Diagnos=" You are an Anorectic  ";
	if ((BMI>15) && (BMI<20)) Diagnos=" You should gain weight  ";
	if ((BMI>19) && (BMI<24)) Diagnos=" You have reached your ideal weight";
	if ((BMI>23) && (BMI<28)) Diagnos=" You are in good health      ";
	if ((BMI>27) && (BMI<32)) Diagnos=" You should loose weight";
	if ((BMI>31) && (BMI<42)) Diagnos=" Health hazrd,  Cardiac  stress";
	if (BMI>41)               Diagnos=" BMI above 42 is rare";

	textBox1->Text=  "Actual Weight = "+Weight+" kg ";
	textBox2->Text =Diagnos;
	textBox3->Text= "Actual Height = "+height+" cm ";
	textBox4->Text= "Actual BMI  = "+BMI+" bmi";
	textBox5->Text= "Weight  = "+Weight2+" kg";
	textBox8->Text= "Weight difference  = "+preferredWeight+" kg";
	textBox9->Text= "bmi difference     = "+bmiDiff+" bmi";

	if (BMI>22){textBox6->Text= "ideal Weight   = 
		"+(Weight-preferredWeight)+" kg";textBox7->Text= "Loose       = 
		"+(preferredWeight) +" kg"; }
	else{ textBox6->Text= "ideal Weight   = "+(Weight+preferredWeight)+" kg";	 
		textBox7->Text= "Gain       = "+(preferredWeight) +" kg"; }

} 
public: void calculateBMI()
{
	if (height>240)
	{
		MessageBox::Show( "Max Height is 240" +"\n"+"\n", 
		"Input Error",MessageBoxButtons::OK ,MessageBoxIcon::Error );
		height=tmp1;textBox10->Text=height+"";
	}
if (Weight>240)
{
	MessageBox::Show( "Max Weight is 240" +"\n"+"\n", 
		"Input Error",MessageBoxButtons::OK ,MessageBoxIcon::Error );
	Weight=tmp2;
	textBox11->Text=Weight+"";
}
BMI= Weight/((height/100)*(height/100));
if (BMI>64000) BMI=64000; setDiagnos();
} 

The getDataFromTextBox() Method

The getDataFromTextBox() method is called upon when data is input from a textbox. And again, the application updates the displays by making calls to setDiagnos() and calculateBMI() to calculate and display the new BMI values.

void getDataFromTextBox()
{
	tmp1=height;tmp2=Weight;
	//height=atoi(str);
	try
	{
		height=System::Convert::ToInt32(textBox10->Text);
	}
	catch ( Exception^ e )
	{
		MessageBox::Show( e->Message +"\n"+"Enter Digits (0-9).\n",
		"Input Error",MessageBoxButtons::OK ,MessageBoxIcon::Error );
		textBox10->Text=tmp1+"";
		height=System::Convert::ToInt32(textBox10->Text);
	}
	try
	{
		Weight=System::Convert::ToInt32(textBox11->Text);
	}
	catch ( Exception^ e )
	{
		MessageBox::Show( e->Message +"\n"+"Enter Digits (0-9).\n",
		"Input Error",MessageBoxButtons::OK ,MessageBoxIcon::Error );
		textBox11->Text=tmp2+"";
		Weight=System::Convert::ToInt32(textBox11->Text);
	}
	calculateBMI();
	updscb();
} 

And that is how easy it is to create a simple BMI calculator in Windows Forms.

Thanks for reading.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here