More VB.Net class homework fun...

Ub3r-L33ch

Member
Joined
Sep 20, 2005
Messages
11
Programming Experience
Beginner
Ok so this goes back to a POST I made before about calculating BMI based on height and weight entered. Well now I have to add to that project.

One part is that I have to include radio buttons to select and if you select the right one with the number range that is equal to the result from the BMI calculation. For instance:

You enter 200 in weight and 74 in inches. The result from the calculation is 25.68, if you selected the radio button 25 to 29.9 you would get a message "You guessed your BMI correctly"

Here's the code I have so far from the previous project with a few new things I added for this one:


'Option Explicit and Strict have to be on in this project
Option Strict On
Option Explicit On

Public Class bmiForm
Inherits System.Windows.Forms.Form

Windows Form Designer Generated Code

'Declare the Constant
Const BMI_Integer As Integer = 703I

Private Sub buttonClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonClear.Click
'Clears the Form and sets focus to Weight text box

textWeight.Clear()
textHeight.Clear()

'Clears the radio Buttons
radButton18.Checked = False
radButton18_24.Checked = False
radButton25_29.Checked = False
radButton30.Checked = False

With textWeight
.Clear()
.Focus()
End With

End Sub

Private Sub buttonExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonExit.Click
'Exits the Form

Me.Close()

End Sub

Private Sub buttonCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonCalc.Click
'Calculates the BMI

'Declare Variables
Dim Weight As Double = Val(textWeight.Text)
Dim Height As Double = Val(textHeight.Text)
Dim BMI As Double = Weight / Height ^ 2 * 703

'Show result with formatting
labelResult.Text = String.Format("{0:n2}", BMI)

'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

End Sub

Private Sub checkBoxInfo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkBoxInfo.CheckedChanged
'Displays additional information when CheckBox is checked

If checkBoxInfo.Checked Then
checkBoxInfo.Text = "Body mss index (BMI) is a measurement based on height and weight as it realtes to body fat. The relationship differs with age and gender. For example, women are more likely to have a higher percent of body fat than men for the same BMI. On average, older people may have more body fat than younger adults with the same BMI."
Else
checkBoxInfo.Text = "Check the box for additional information"
End If

End Sub

End Class

Sorry to bug you guys with such simple crap, hopefully it wont take more than a few minutes to explain to me what I need to do.

Thanks for any help.
 
Ok I got a little bit more done on this project, here is what I have:

Private Sub buttonCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonCalc.Click
'Calculates the BMI

'Declare Variables
Dim WeightInteger, HeightInteger As Integer
Dim BMI As Double

'Convert input values to numeric variables
WeightInteger = Int16.Parse(textWeight.Text)
HeightInteger = Int16.Parse(textHeight.Text)

'Calculate the values
BMI = WeightInteger / HeightInteger ^ 2 * BMI_Integer

'Show result with formatting
labelResult.Text = String.Format("{0:n2}", BMI)

'Display BMI Rating
If BMI < 18.5 And radButton18.Checked = True Then
labelRateDisplay.Text = "Underweight"
ElseIf BMI > 18.5 And BMI <= 24.9 And radButton18_24.Checked = True Then
labelRateDisplay.Text = "Normal"
ElseIf BMI > 25 And BMI <= 29.9 And radButton25_29.Checked = True Then
labelRateDisplay.Text = "Overweight"
ElseIf BMI > 30 And BMI <= 34.9 And radButton30_34.Checked = True Then
labelRateDisplay.Text = "Obesity I"
ElseIf BMI > 35 And BMI <= 39.9 And radButton35_39.Checked = True Then
labelRateDisplay.Text = "Obesity II"
ElseIf BMI > 40 And radButton40.Checked = True Then
labelRateDisplay.Text = "Danger, Explosion Iminent!"
End If

Now what I need is to display a message box if they have the correct radio button selected.

What I want to do is have a message box come up and say "You guessed your BMI correctly" with an ok button.

If they selected the wrong one I want to display is
"You guessed your BMI incorrectly. Do you want to re-enter the values?"
Then it will have a Retry and Cancel button. Retry clears the form and cancel exits the form.
 
once you have all the BMI calculations done all you would need is a boolean variable (something like blnGuessedCorrectly) and have it equal false before checking

then as you check each one if they correctly selected their BMI, change the variable to True

then at the end simply check that variable:
VB.NET:
If blnGuessedCorrectly = True Then
  Messagebox.Show("Correct BMI selection", "Correct", MessageBoxButtons.OK)
Else
  If Messagebox.Show("Incorrect BMI selection", "Incorrect", MessageBoxButtons.RetryCancel) = DialogResult.Cancel Then
    Me.Close()
  Else
    'Clear the form here for them to re-enter input
  End If
End If
you may need to modify this, but this is the idea
 
Ah thank you very much, I will work with this, I am almost done with this silly project... at least I'm learning though.

Edit: Well, I now know how to declare a boolean and set it equal to false but I'm not sure where to put it in my code to set it equal to true if the correct BMI is guessed.
 
Last edited by a moderator:
Nevermind I figured it out. :)

Last thing I need to do is call my clear button procedure, not sure how to do that.

This is the code for it:

Private Sub buttonClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonClear.Click
'Clears the Form and sets focus to Weight text box

textWeight.Clear()
textHeight.Clear()
labelResult.ResetText()
labelRateDisplay.ResetText()

'Clears the radio Buttons
radButton18.Checked = False
radButton18_24.Checked = False
radButton25_29.Checked = False
radButton30_34.Checked = False
radButton35_39.Checked = False
radButton40.Checked = False

With textWeight
.Clear()
.Focus()
End With

End Sub

I'm sure I just have to call buttonClear_Click somehow.




Edit: Ok I got everything done. I just need help with 1 more thing.

If my boolean variable ends up at false (meaning they entered nothing and clicked none of the radio buttons) how do I tell it to not run my If statement for the message boxes. Right now if I enter nothing it still comes up saying "You guessed incorrectly", I dont want it to pop up at all if nothing is entered.

Thanks
 
Last edited:
there are two ways to do that:
(1) in the editor make one of the RadioButtons checked so as far as the program is concerned, the user picked one (you forced them to)

(2) you can have a Module-Level variable (declared as Private under the " windows generated code " part) and set that to false then in each of the RadioButton's CheckedChanged events (just double click these) make that Module level variable = true
then put this:
VB.NET:
If blnGuessedCorrectly = True Then
  Messagebox.Show("Correct BMI selection", "Correct", MessageBoxButtons.OK)
Else
  If Messagebox.Show("Incorrect BMI selection", "Incorrect", MessageBoxButtons.RetryCancel) = DialogResult.Cancel Then
    Me.Close()
  Else
    'Clear the form here for them to re-enter input
  End If
End If
inside:
VB.NET:
If mblnUserPicked = True Then

End If
which means the user picked one then display the correct/incorrect messagebox

also to call a button's click just use the PerformClick() method of the button
Button1.PerformClick()
 
Alright I got everything working now, thanks for all your help! :)

I promise you I will be back though... lol, not even half way through the semester.
 
Back
Top