How to create a BMI calculator, English and Metric:
First steps:
First you need to add 2 numeric up and down boxes to the form.
name them numWeight and numHeight, make sure you change the maximum number in numWeight to 350 (Because it's 100 and i'm sure everyone weighs over 100 pounds.)
Make sure for decimal places you put 2 and increment you put either 1 or .5 your choice.
Now create two labels name weight and height, position the labels so they correspond with the correct numeric boxes.
Now make them say Weight(Pounds) and Height(Inches).
Next we're going to add the function that does the math for the english system. Double click your form and add this code:
Code:
Public Sub Calculate_english()
Dim Result As Double
Dim Rounded_Result As Double
Result = numWeight.Value / (numHeight.Value * numHeight.Value) * 703 ' this is saying to take the value from the numWeight box and divide it by the numHeight value squared (numHeight*numHeight) times 703
Rounded_Result = Math,Round(Result, 1) ' Rounds the result of the BMI to the first decimal. (Math,Round change the comma to a period. It was block because it thought it was an url or something.)
bmi.Text = Rounded_Result.ToString ' This is saying that the label bmi (we'll create this later) is going to be the rounded result.
BMI_Message(Rounded_Result) 'this is for the message system (underweight, etc..) we'll do the code for that later.)
End Sub
Next we're going to create the function for the metric system: (Note i copied and pasted this from the english function above so i didn't have to redo the colors.. so make sure everything english is changed to metric if i forgot it. DO NOT ADD * 703. THE METRIC SYSTEM FOR BMI IS DIFFERENT IT IS JUST WEIGHT DIVIDED BY HEIGHT SQUARED.
Code:
Public Sub Calculate_metric()
Dim Result As Double
Dim Rounded_Result As Double
Result = numWeight.Value / (numHeight.Value * numHeight.Value)' this is saying to take the value from the numWeight box and divide it by the numHeight value squared (numHeight*numHeight).
Rounded_Result = Math,Round(Result, 1) ' Rounds the result of the BMI to the first decimal. (Math,Round change the comma to a period. It was block because it thought it was an url or something.)
bmi.Text = Rounded_Result.ToString ' This is saying that the label bmi (we'll create this later) is going to be the rounded result.
BMI_Message(Rounded_Result) 'this is for the message system (underweight, etc..) we'll do the code for that later.)
End Sub
No we are going to create the message system which is to determine whether the user is underweight, overweight, normal or obese.
Ignore the message.text we'll add label later so you may get so errors just entering this. Don't change anything unless you know what your doing.
Code:
Public Sub BMI_Message(ByVal BMI_Value As Double)
Select Case BMI_Value
Case 0.0 To 18.5 'if the BMI value is ranged from 0.0 to 18.5 then display the message "Underweight"
message.Text = "Underweight"
Case 18.6 To 24.9 'if the BMI is ranged from 18.6 to 24.9 then display the message "Normal Weight"
message.Text = "Normal Weight"
Case 25.0 To 29.9 'if the BMI is ranged from 25.0 to 29.9 then display the message "Overweight"
message.Text = "Overweight"
Case Is >= 30.0 'if the BMI is ranged from 30.0 and above then display the message "Obese"
message.Text = "Obese"
Case Else 'if the BMI is ranged with anyother number or is lower then 0 somehow or some random shit then display"unable to determine BMI result"
message.Text = "Unable to Determine BMI Result"
End Select
End Sub
Now we're going to create 2 more labels and then name them bmi and message, and in properties change visible true to visible false. Don't worry about changing the text. the program will do that for us!
Next we're going to create 2 radio buttons, name one english and one metric. Text for english is going to be English, and for metric it's going to be Metric. These are so the user can choose whether he wants to measure their BMI in the metric or english system.
Double click the english radio button and add this code.
Code:
Private Sub english_CheckedChanged(sender As Object, e As EventArgs) Handles english.CheckedChanged
If english.Checked Then 'if the english radio button is check then
metric.Checked = False 'the metric radio button is not checked
weight.Text = "Weight(Pounds)" 'weight label is going to be weight(Pounds)
height.Text = "Height(Inches)" 'height label is going to be height(inches)
numWeight.Value = "100"
numHeight.Value = "50"
End If
End Sub
Now double click the metric radio button and add:
Code:
Private Sub metric_CheckedChanged(sender As Object, e As EventArgs) Handles metric.CheckedChanged
If metric.Checked Then 'if the metric radio button is check then
english.Checked = False 'the english radio button is not checked
weight.Text = "Weight(Kilograms)" 'weight label is going to be weight(kilograms)
height.Text = "Height(Meters)" 'height label is going to be height(meters)
numWeight.Value = "40"
numHeight.Value = "1.00"
End If
End Sub
Ok now when you debug the program when you check metric system or english system the labels should change according to which one is checked. If not then run through those codes and check for mistakes.
Finally we are going to add a button leave the name how it is, and change the text to calculate then double click it and add this code:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If english.Checked = True Then
Try
Calculate_english()
MsgBox("Your BMI (Body mass indicator) has been calculated in the English system.", MsgBoxStyle.Exclamation, "Calculated!")
bmi.Visible = True
message.Visible = True
Catch ex As Exception
MsgBox("Error - " & ex.Message, MsgBoxStyle.Critical, "BMI Calculator Error Has occured.")
End Try
End If
If metric.Checked = True Then
Try
Calculate_metric()
MsgBox("Your BMI (Body mass indicator) has been calculated in the Metric system.", MsgBoxStyle.Exclamation, "Calculated!")
Catch ex As Exception
MsgBox("Error - " & ex.Message, MsgBoxStyle.Critical, "BMI Calculator Error Has occured.")
End Try
End If
End Sub
After that Debug it and test it out!
Have any errors or concerns post here! If i helped thank me!
NO NEGATIVE FEEDBACK PLEASE IDC WHAT YOU THINK IF IT'S NEGATIVE..