|
-
May 1st, 2009, 11:03 AM
#1
Thread Starter
Junior Member
BMI Program
I'm making a program which calculates a user's BMI (Body Mass Index). I am fairly new to VB.NET so please bare with me.
This is what I have so far...
Code:
Public Class BMI
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'an integer is one or a group of characters has a value ranging from 0 to 255
Dim number1 As Decimal 'user enters height
Dim number2 As Integer 'user enters weight
Dim answer As Decimal 'signals that the answer must be a decimal
'Val returns the numbers contained in a string as a numeric value of appropriate type
number1 = Val(TextBox1.Text) 'number 1 equals TextBox1
number2 = Val(TextBox2.Text) 'number 2 equals TextBox2
answer = number2 / (number1 * 2) 'this shows what the calculation is and both number1 and number2 will be added together
MsgBox(answer) 'a message box will appear with the user's BMI
End Sub
End Class
The concept works absolutely fine, however, I want to extend it a bit.
I'd like to include some sort of validation, so the user is permitted only to use numbers and not letters. So if they did enter a letter or having said that, if they enter anything in the wrong format, an error message will appear.
I'd also like the pop-up box with the user's BMI to say whether they are under, over, or a healthy weight.
Could anybody help me with this?
-
May 1st, 2009, 11:19 AM
#2
Junior Member
Re: BMI Program
You could use a MaskedTextBox, setting the mask to the format you need.
By the way, the body mass index is calculated as Weight / (Height * Height)
http://en.wikipedia.org/wiki/Body_mass_index
-
May 1st, 2009, 12:35 PM
#3
Re: BMI Program
'an integer is one or a group of characters has a value ranging from 0 to 255
You may want to work on your definition of an integer.
-
May 1st, 2009, 12:41 PM
#4
Re: BMI Program
 Originally Posted by keystone_paul
You may want to work on your definition of an integer.
Well he's not exactly wrong... just unaware. An 8bit unsigned integer has a range of 0-255. In VB.net however its a 32 bit signed integer so it's range is −2,147,483,648 to +2,147,483,647...
-
May 1st, 2009, 01:09 PM
#5
Re: BMI Program
An 8bit unsigned integer has a range of 0-255. In VB.net however its a 32 bit signed integer so it's range is −2,147,483,648 to +2,147,483,647...
Indeed, however as its a VB.net program and preceding the declaration of a variable of type integer, it is at best misleading.
In my view the only thing worse than a program without comments is one with misleading comments.
-
May 1st, 2009, 05:20 PM
#6
Thread Starter
Junior Member
Re: BMI Program
Right, I've used masked text boxes for height and weight as KaDMiO said to do.
Now, I need to know how I can adjust the BMI decimal answer so that there is only one number after the decimal point rather than something like 18.4444444444444444444444.
And, how do I go about doing validation, so the user is told whether they are over, under, or a healthy weight?
-
May 1st, 2009, 05:27 PM
#7
Junior Member
Re: BMI Program
To do that, use the Math.Round function.
For the over, under or healthy weight, If clauses.
-
May 1st, 2009, 09:08 PM
#8
Member
Re: BMI Program
If your having further problems theres plenty examples of BMI calculators on Youtube and Google.
-
May 2nd, 2009, 02:06 PM
#9
Thread Starter
Junior Member
Re: BMI Program
 Originally Posted by Jericho
If your having further problems theres plenty examples of BMI calculators on Youtube and Google.
That's very helpful. Funnily enough, I have looked around on the internet but couldn't find anything which helped me.
Could anyone show me how to implement the Math.Round function into the code I've given? Because at the moment I'm having trouble doing it despite looking on Google for ages. I thought it would be easy to do.
-
May 2nd, 2009, 04:39 PM
#10
Re: BMI Program
The second parameter sets how many decimal places.
Code:
Dim tempDec As Decimal
tempDec = Math.Round(18.4444444444444444444444, 1)
-
May 3rd, 2009, 01:36 AM
#11
Member
Re: BMI Program
 Originally Posted by ACM1989
That's very helpful.  Funnily enough, I have looked around on the internet but couldn't find anything which helped me.
Could anyone show me how to implement the Math.Round function into the code I've given? Because at the moment I'm having trouble doing it despite looking on Google for ages. I thought it would be easy to do.
Really? Cause I just looked then and look what I found!
http://www.vbtutor.net/vb2008/vb2008_lesson7.html
-
May 3rd, 2009, 05:20 PM
#12
Thread Starter
Junior Member
Re: BMI Program
No help mate. It doesn't answer the question I asked.
-
May 4th, 2009, 10:19 AM
#13
Junior Member
Re: BMI Program
answer = Math.Round(number2 / (number1 * number1),1)
That should work.
-
May 4th, 2009, 11:08 AM
#14
Hyperactive Member
Re: BMI Program
If you are rounding just because you want to limit the numbers displayed after decimal point, you can also use the Format function to do that, and still retain the multiple decimals for accuracy in other calcs.
Eg.
Code:
answer = WeightInKg / ((HeightInCm /100)^2)
MsgBox("BMI: " & Format(answer, "#,##0.00") & " kg/m2", MsgBoxStyle.OkOnly, "BMI Result")
Runesmith
The key to getting the right answer is asking the right question
I just realized: good health is merely the slowest possible rate at which one can die
-
May 4th, 2009, 04:41 PM
#15
Thread Starter
Junior Member
Re: BMI Program
Thanks guys, I used KaDMio's and it works just fine.
I'm immensely stuggling though to do IF statements for a message box. I need the message box to not only display the user's BMI but also whether they are healthy or over or underweight.
-
May 4th, 2009, 08:00 PM
#16
New Member
Re: BMI Program
Something like this will probably work..
Code:
dim LOWBMI as Integer = 20
dim HIGHBMI as Integer = 30
if answer < LOWBMI then
MsgBox("Start eating more pies, your BMI is " & answer)
elseif answer > HIGHBMI then
MsgBox("You're too fat: your BMI is " & answer)
else
MsgBox("You're as normal as it gets: your BMI is " & answer)
endif
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|