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

A Simple Metrics System BMI Calculator in Windows Forms

, 29 May 2010
Rate this:
Please Sign up or sign in to vote.
A simple metrics system BMI calculator in Windows Forms


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++.


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


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)
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 );
if (Weight>240)
	MessageBox::Show( "Max Weight is 240" +"\n"+"\n", 
		"Input Error",MessageBoxButtons::OK ,MessageBoxIcon::Error );
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()
	catch ( Exception^ e )
		MessageBox::Show( e->Message +"\n"+"Enter Digits (0-9).\n",
		"Input Error",MessageBoxButtons::OK ,MessageBoxIcon::Error );
	catch ( Exception^ e )
		MessageBox::Show( e->Message +"\n"+"Enter Digits (0-9).\n",
		"Input Error",MessageBoxButtons::OK ,MessageBoxIcon::Error );

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

Thanks for reading.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

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.

Comments and Discussions

GeneralMy vote of 3 PinprofessionalManikandan1024-Jun-14 20:54 
GeneralMy vote of 1 PinmemberTobiasP2-Dec-10 2:47 
GeneralMy vote of 1 PinmemberMember 15069667-Jun-10 6:31 
GeneralCompiled version PinmemberJohnny J.20-May-10 1:49 
Could you supply a compiled version, please?
I don't have C++ installed on my computer...
GeneralMy vote of 2 Pinmemberbryce11-May-10 13:38 
GeneralRe: My vote of 2 PinmemberTopCoder2315-May-10 7:12 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 30 May 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid