VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Coder

BMI Calculator

Calculate your Body Mass Index and understand your health status


Body Mass Index (BMI) is a simple calculation using a person's height and weight. The formula is BMI = kg/m2 where kg is a person's weight in kilograms and m2 is their height in meters squared.

A high BMI can indicate high body fatness. BMI screens for weight categories that may lead to health problems, but it does not diagnose the body fatness or health of an individual.

BMI Categories

  • Underweight: BMI less than 18.5
  • Normal weight: BMI between 18.5 and 24.9
  • Overweight: BMI between 25 and 29.9
  • Obesity: BMI of 30 or greater
BMI Scale

BMI Calculator

Enter your details below to calculate your BMI and understand your weight category.

Your BMI Result

--
Enter your details
Underweight Normal Overweight Obese

Visual Basic Code Examples

VB6 Implementation

This is the original VB6 implementation of the BMI Calculator:

Private Sub Command1_Click()
    Label4.Caption = BMI(Text1.Text, Text2.Text)
End Sub

Private Function BMI(height, weight)
    BMIValue = (weight) / (height ^ 2)
    BMI = Format(BMIValue, "0.00")
End Function

The VB6 version calculates BMI using the standard formula and displays the result formatted to two decimal places.

VB.NET Implementation

This is the modern VB.NET implementation with category detection:

Public Class BMICalculator
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim height As Double = Double.Parse(txtHeight.Text)
        Dim weight As Double = Double.Parse(txtWeight.Text)
        
        Dim bmi As Double = CalculateBMI(height, weight)
        Dim category As String = GetBMICategory(bmi)
        
        lblResult.Text = $"Your BMI: {bmi:F2}"
        lblCategory.Text = $"Category: {category}"
        lblCategory.BackColor = GetCategoryColor(category)
    End Sub

    Private Function CalculateBMI(height As Double, weight As Double) As Double
        Return weight / (height * height)
    End Function

    Private Function GetBMICategory(bmi As Double) As String
        Select Case bmi
            Case Is < 18.5 : Return "Underweight"
            Case 18.5 To 24.9 : Return "Normal weight"
            Case 25 To 29.9 : Return "Overweight"
            Case Else : Return "Obesity"
        End Select
    End Function

    Private Function GetCategoryColor(category As String) As Color
        Select Case category
            Case "Underweight" : Return Color.LightBlue
            Case "Normal weight" : Return Color.LightGreen
            Case "Overweight" : Return Color.Orange
            Case Else : Return Color.LightCoral
        End Select
    End Function
End Class

The VB.NET version enhances the original with category detection and color coding.

Understanding Your BMI

BMI is a useful measurement for most people over 18 years old. But it is only an estimate and it doesn't take into account age, ethnicity, gender and body composition.

Limitations of BMI

  • Muscle mass: BMI doesn't distinguish between muscle and fat, so athletes may have a high BMI without health risks
  • Age: The same BMI may indicate different levels of body fat at different ages
  • Ethnicity: Some ethnic groups have different associations between BMI, percentage of body fat, and health risks
  • Pregnancy: BMI calculations are not suitable for pregnant women

BMI Recommendations

  • For most adults, an ideal BMI is in the 18.5 to 24.9 range
  • For children and young people aged 2 to 18, BMI calculation considers age and gender
  • If you have a high BMI, losing weight could help reduce your risk of weight-related diseases
  • If you have a low BMI, you may need to gain weight to improve your health

Note: This calculator provides general information only. For health advice, please consult a medical professional.