Results 1 to 9 of 9

Thread: BMI Program Help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    BMI Program Help

    Okay, here's my code so far for a BMI program I'm making. It's almost completed but there's just or or two areas which need correcting.

    At the moment, if the user doesn't enter a height or weight, or only enters a weight and not a height, an error appears called System.DivideByZeroException.

    My question is, how can I have some sort of window which pops up telling the user to enter a height or weight when they click the submit button without having inputted anything, or if they've left out one field?

    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(MaskedTextBox1.Text) 'number 1 equals TextBox1
            number2 = Val(MaskedTextBox2.Text) 'number 2 equals TextBox2
    
            answer = Math.Round(number2 / (number1 * number1), 1) 'this shows what the calculation is. number2 (weight) is divided by number1 x number1 (height)
    
            'sets the variable "LOWBMI" and "HIGHBMI" as whole numbers
            Dim LOWBMI As Integer = 18.5
            Dim HIGHBMI As Integer = 25
    
            'if the answer equals less than what the LOWBMI is then display "You are underweight, your BMI is..."
            If answer < LOWBMI Then
                MsgBox("You are underweight, your BMI is " & answer)
                'if the answer equals more than what the HIGHBMI is then display "You are overweight, your BMI is..."
            ElseIf answer > HIGHBMI Then
                MsgBox("You are overweight, your BMI is " & answer)
                'or in any other case display "You are a healthy weight"
            Else
                MsgBox("You are a healthy weight, your BMI is " & answer)
            End If
    
        End Sub
    
    End Class

  2. #2
    Addicted Member
    Join Date
    Apr 2009
    Location
    Near Dog River (sorta)
    Posts
    160

    Re: BMI Program Help

    The really simple method is:

    vb.net Code:
    1. Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
    2.     If MaskedTextBox1.Text = "" OrElse MaskedTextBox2.Text = "" Then
    3.         MessageBox.Show("Required information is missing.", "Information Missing", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
    4.     Else
    5.         ' process user input here
    6.     End If
    7. End Sub
    PC #1: Athlon64 X2+Vista64+VS2008EE+.NET3.5 #2: XP (all flavours Ghost'd)+VS2008EE+.NET3.5
    Help Files: HelpMaker · Dr. Explain · Create Icons: IcoFX


    "Whoever eats my flesh and drinks my blood has eternal life, and I will raise him on the last day." John 6:54

  3. #3
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: BMI Program Help

    why do you need a message box? You could validate the text boxes as text is entered, and enable/disable the button as appropriate.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  4. #4
    Addicted Member
    Join Date
    Apr 2009
    Location
    Near Dog River (sorta)
    Posts
    160

    Re: BMI Program Help

    Do you mean like this? Change the 'Is Valid" to suit your needs.

    vb.net Code:
    1. Private Sub MaskedTextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.Leave
    2.     If MaskedTextBox1.Text Is Valid AndAlso MaskedTextBox1.Text Is Valid Then
    3.         Button1.Enabled = True
    4.     Else
    5.         Button1.Enabled = False
    6.     End If
    7. End Sub
    8.  
    9. Private Sub MaskedTextBox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaskedTextBox2.Leave
    10.     If MaskedTextBox1.Text Is Valid AndAlso MaskedTextBox1.Text Is Valid Then
    11.         Button1.Enabled = True
    12.     Else
    13.         Button1.Enabled = False
    14.     End If
    15. End Sub
    PC #1: Athlon64 X2+Vista64+VS2008EE+.NET3.5 #2: XP (all flavours Ghost'd)+VS2008EE+.NET3.5
    Help Files: HelpMaker · Dr. Explain · Create Icons: IcoFX


    "Whoever eats my flesh and drinks my blood has eternal life, and I will raise him on the last day." John 6:54

  5. #5
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: BMI Program Help

    I would use the TextChanged event - I think the masked text box has that, but I never use them, so am not sure.

    The TextChanged event is nice because it gives the user immediate feedback based on their actions, rather than trying to click a button which goes 'disabled' when they try to click it.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  6. #6
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: BMI Program Help

    You are giving the user too much freedom. Use 3 numeric UpDowns. This way only numebrs can be entered and since the decimal sign is "." in some countries and "," in others, you can eliminate a great deal of messy code when this problem becomes apparent.

    Before the calculation you can check if the value of any of the NumUpDowns is problematic and show a message about that.
    VB 2005, Win Xp Pro sp2

  7. #7
    Addicted Member
    Join Date
    Apr 2009
    Location
    Near Dog River (sorta)
    Posts
    160

    Re: BMI Program Help

    Quote Originally Posted by Half View Post
    You are giving the user too much freedom. Use 3 numeric UpDowns. This way only numebrs can be entered and since the decimal sign is "." in some countries and "," in others, you can eliminate a great deal of messy code when this problem becomes apparent.

    Before the calculation you can check if the value of any of the NumUpDowns is problematic and show a message about that.
    I've always been told that numericUpDowns are the armpit of user controls.
    PC #1: Athlon64 X2+Vista64+VS2008EE+.NET3.5 #2: XP (all flavours Ghost'd)+VS2008EE+.NET3.5
    Help Files: HelpMaker · Dr. Explain · Create Icons: IcoFX


    "Whoever eats my flesh and drinks my blood has eternal life, and I will raise him on the last day." John 6:54

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: BMI Program Help

    Quote Originally Posted by JugglingReferee View Post
    The really simple method is:

    vb.net Code:
    1. Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
    2.     If MaskedTextBox1.Text = "" OrElse MaskedTextBox2.Text = "" Then
    3.         MessageBox.Show("Required information is missing.", "Information Missing", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
    4.     Else
    5.         ' process user input here
    6.     End If
    7. End Sub
    This one works fine, but the problem is that although the error window appears if the user clicks the submit button without having typed in anything, but once you click 'OK' in that window, the error message appears again called "System.DivideByZeroException" and it highlights in yellow this part of the code: "answer = Math.Round(number2 / (number1 * number1), 1)".

  9. #9
    Addicted Member
    Join Date
    Apr 2009
    Location
    Near Dog River (sorta)
    Posts
    160

    Re: BMI Program Help

    Quote Originally Posted by ACM1989 View Post
    This one works fine, but the problem is that although the error window appears if the user clicks the submit button without having typed in anything, but once you click 'OK' in that window, the error message appears again called "System.DivideByZeroException" and it highlights in yellow this part of the code: "answer = Math.Round(number2 / (number1 * number1), 1)".
    Perhaps to change the code to also not allow entered values of zero?
    PC #1: Athlon64 X2+Vista64+VS2008EE+.NET3.5 #2: XP (all flavours Ghost'd)+VS2008EE+.NET3.5
    Help Files: HelpMaker · Dr. Explain · Create Icons: IcoFX


    "Whoever eats my flesh and drinks my blood has eternal life, and I will raise him on the last day." John 6:54

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width