School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,120 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,955 people online right now. Registration is fast and FREE... Join Now!




Create a BMI (Body Mass Index) Calculator

 
Reply to this topicStart new topic

> Create a BMI (Body Mass Index) Calculator

PDUNZ
Group Icon



post 20 Aug, 2009 - 05:39 AM
Post #1


Right, today I thought I'd do something a bit different and show you how to make a BMI (Body Mass Index) Calculator. Just something that is a bit more fun, but also show you how you can use Select Case and (ByRef, ByVal).

Just a personal note: I like to use seperate functions/subs - you dont have to, but I find it easier and more organised.

Right, the first step I took in creating this was to look for the forumla to calculate the BMI, so a quick search on Google showed that BMI is calculated as follows.

BMI = Weight (lbs) devided by Height Squard and multiply by 703.

So with that in hand, lets get cracking.

So, first off - we know that it requires Weight and Height, so I am going to use 2 NumericUpDown controls. Add 2 to your form, and also 2 lables Weight (lbs) and Height (Inch). I also gave the 2 Numeric controls names to make it easier, numWeight and numHeight.

We also need a means of showing the result on screen, so add a Groupbox, and 2 more labels. Change the Groupbox Text to Result, and name one label lblBMI and the other lblMessage. We are going to use these to show the BMI value and the
appropiate message back to the user. And lastly, add a button and give it a name Calculate.

This is what I have so far.


Now we need to set some limits for numWeight and numHeight. Set the properties as follows.
numWeight: Min 100, Max 300, Value 130.5, Decimal Places 1, Increment 0.5.
numHeight: Min 36, Max 95, Value 70.0, Decimal Places 1, Increment 0.5.

Right, now for the first part - the Calculate Sub which is used to do the calculations.
Add the following to your editor. There is also a comment for each line to give you an idea
on what it does.

CODE

Public Sub Calculate()
        Dim Result As Double ' Declare "Result" as a Double.
        Dim Rounded_Result As Double

        Result = numWeight.Value / (numHeight.Value * numHeight.Value) * 703 ' BMI Calculation.

        Rounded_Result = Math.Round(Result, 1) ' Round result to 1 decimal place.
        lblBMI.Text = Rounded_Result.ToString ' Display BMI using lblBMI.

        BMI_Message(Rounded_Result) 'Send Rounded BMI result to Sub BMI_Message.

    End Sub


Now, we move on to the BMI_Message Sub which is what displays the various messages back to the user.
Add the following to your code.
CODE

Public Sub BMI_Message(ByVal BMI_Value As Double)
        Select Case BMI_Value
            Case 0.0 To 18.5
                lblMessage.Text = "Underweight"
            Case 18.6 To 24.9
                lblMessage.Text = "Normal Weight"
            Case 25.0 To 29.9
                lblMessage.Text = "Overweight"
            Case Is >= 30.0
                lblMessage.Text = "Obese"
            Case Else
                lblMessage.Text = "Unable to Determin BMI"
        End Select
    End Sub


The code above uses the rounded value calculated in the Calculate sub and by using a Select Case - determins what message to display back to the user using the lblMessage label.

And now just one more line of code, add Calculate() to the Calculate Button Click Event, this calls the Calculate Sub.

Thats pretty much it, save and then run the application - enter some values and hit Calculate. You should have similar to the picture below.


Below is the whole code for the project, from beginning to end.
CODE

Option Strict On
Option Explicit On
''' <summary>
''' Tutorial, BMI Calculator.
''' Dream.In.Code PDUNZ.
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Public Sub Calculate()
        Dim Result As Double ' Declare "Result" as a Double.
        Dim Rounded_Result As Double

        Result = numWeight.Value / (numHeight.Value * numHeight.Value) * 703 ' BMI Calculation.

        Rounded_Result = Math.Round(Result, 1) ' Round result to 1 decimal place.
        lblBMI.Text = Rounded_Result.ToString ' Display BMI using lblBMI.

        BMI_Message(Rounded_Result) 'Send Rounded BMI result to Sub BMI_Message.

    End Sub

    Public Sub BMI_Message(ByVal BMI_Value As Double)
        Select Case BMI_Value
            Case 0.0 To 18.5
                lblMessage.Text = "Underweight"
            Case 18.6 To 24.9
                lblMessage.Text = "Normal Weight"
            Case 25.0 To 29.9
                lblMessage.Text = "Overweight"
            Case Is >= 30.0
                lblMessage.Text = "Obese"
            Case Else
                lblMessage.Text = "Unable to Determin BMI"
        End Select
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Calculate()
    End Sub
End Class


Thanks for reading.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

PDUNZ
Group Icon



post 20 Aug, 2009 - 04:55 PM
Post #2
Screenshots (Forgot to add them when I transfered sorry).
Attached Image

Attached Image
Go to the top of the page
+Quote Post

jcremeans11
**



post 8 Oct, 2009 - 07:15 AM
Post #3
Thanks alot, I was actually creating a fitness program with .net so this was perfect smile.gif
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 01:51PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month