College Class - Help with project, simple calculation.

Ub3r-L33ch

Member
Joined
Sep 20, 2005
Messages
11
Programming Experience
Beginner
I'm supposed to be creating a form to calculate body mass index (BMI) by doing:

BMI = Weight In Pounds / (Height in Inches) ^ 2 * 703

I have to create two text boxes to store the information in and then use that information for my calculation, I have that part done ok I think. Then display the result in a label box.

I need to know how to then do the calculation. We are supposed to have both Option Explicit and Option Strict On.

I'm down to declaring the variables but I'm sort of lost from there.

Thanks for any help.
 
Ok this is the code that you need but for just in case i attached the project as well

VB.NET:
[size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Button1_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] Button1.Click
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Weight [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Double[/color][/size][size=2] = Val(txtWeight.Text)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Height [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Double[/color][/size][size=2] = Val(txtHeight.Text)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] BMI [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Double[/color][/size][size=2] = Weight / Height ^ 2 * 703
 
lblResult.Text = [/size][size=2][color=#0000ff]String[/color][/size][size=2].Format("{0:n2}", BMI) [/size][size=2][color=#008000]'you can avoid formating but you will have a result like 1,8575757575 instead 1,86
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
[/color][/size]

Regards ;)
 

Attachments

  • BMI.zip
    23.9 KB · Views: 28
Sweet! Thank you very much! Alot of this stuff is hard for me to get it in to my brain even though its simple stuff but hopefully one day I'll get the hang of it :)
 
I have another question, if you dont mind helping again.

We have to use Try...Catch to catch errors when entering weight and height.

I got some running for catching non-numeric data but I cant figure out how to add another one for each text box that will catch with dividebyZeroException.

This is what I used for the non-numeric catch:

'Catch Errors in Weight Text Box
Try
Weight = Integer.Parse(textWeight.Text)
Catch err As FormatException
MessageBox.Show("Non-numeric Data entered in Weight.", "Error", MessageBoxButtons.OK)
End Try

'Catch Errors in Height Text Box
Try
Height = Integer.Parse(textHeight.Text)
Catch err As FormatException
MessageBox.Show("Non-numeric Data entered in Height.", "Error", MessageBoxButtons.OK)
End Try

Thanks again.
 
Well, i suppose it is much better if you handle that in advance ... with other words to disable non-numeric text being entered in weight and height.
VB.NET:
[size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] disable_KeyDown([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.KeyEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] textWeight.KeyDown, textHeight.KeyDown
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] e.KeyCode = [/size][size=2][color=#0000ff]Char[/color][/size][size=2].IsDigit(","c) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]e.Handled = [/size][size=2][color=#0000ff]False
 
[/color][/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2]e.Handled = [/size][size=2][color=#0000ff]True
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size]

HTH

Regards ;)
 
Thank you, that will work. Unfortunately I think my professor wants us to use the try catch blocks, he may accept this way but take some points off.

Anyway you could show me how to do it with the try catch blocks?

Thanks again for all your help.
 
Back
Top