Click here to Skip to main content
Click here to Skip to main content

A Simple Metrics System BMI Calculator in Windows Forms

By , 29 May 2010
 

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Clark Kent SuperCoder
Sweden Sweden
Member
About me:
I attended programming School and I have a degree in three programming languages.
C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
I am a professional, I've gotten paid to teach coding. I am roughly 20 years old and i have been a teacher's assistant in programming ,
i have held a lecture in Visual basic programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.
 
I've written about a dozen small simple applications and games.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 2memberbryce11 May '10 - 13:38 
The question i have is - What have you taught us here?
GeneralRe: My vote of 2 PinmemberTopCoder2315 May '10 - 7:12 
BMI, i hope.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 30 May 2010
Article Copyright 2010 by Clark Kent SuperCoder
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid