r/visualbasic • u/MelloYelloSurge • Oct 14 '20
Stumped with formula in code.
Apologies in advance if a) I come off as a total noob (I'm new to Visual Basic) and b) if this post is rather long. TL;DR: I need to calculate BMI using the values entered into the textbox objects I'm using and spit out the answer onto one of the labels.
So I find myself stumped on setting up a set of calculations to determine Body Mass Index (BMI) using either English units (pounds and inches) or Metric units (metres and kilograms). Here is the code I have for this part, so far:
'Here's where I attempt the BMI calculations. Wish me luck.
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
lblBMIHeader.Visible = True
lblBMI.Visible = True
'Here are the declarations. I'm seriously hoping I got this part right.
Dim decUS As Decimal
Dim decMetric As Decimal
Dim decHeight As Decimal
Dim decWeight As Decimal
If radUS.Checked = True Then 'The process that uses pounds and inches
decHeight = Convert.ToDecimal(txtHeight.Text)
decWeight = Convert.ToDecimal(txtWeight.Text)
If decHeight > 0 And decWeight > 0 Then
decUS = (decWeight / decHeight ^ 2) * 703
lblBMI.Text = decUS.ToString("00.0")
ElseIf (decHeight <= 0 And decWeight <= 0) Or (decHeight <= 0 Or decWeight <= 0) Then
MsgBox("Please try again. Only numbers greater than 0 should be in this field.", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
Else
MsgBox("Please enter a positive number in the text boxes.", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
End If
ElseIf radMetric.Checked = True Then 'The process that uses kilograms and metres
decHeight = Convert.ToDecimal(txtHeight.Text)
decWeight = Convert.ToDecimal(txtWeight.Text)
If decHeight > 0 And decWeight > 0 Then
decMetric = decWeight / decHeight ^ 2
lblBMI.Text = decMetric.ToString("00.0")
ElseIf (decHeight <= 0 And decWeight <= 0) Or (decHeight <= 0 Or decWeight <= 0) Then
MsgBox("Please enter a number greater than 0", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
Else
MsgBox("Please enter a positive number in the text boxes.", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
End If
End If
End SubAnd here is what the interface looks like in the designer (the above code is tied to the 'Calculate' button):

I have more to do with this program before it's completed but I'm stumped with how to calculate the values entered into the textbox objects. I thought that linking the values with 'And' statements would be the best way to solve the problem but it didn't work. I'm not sure how to proceed from here and was wondering if anyone here had any ideas?
Update: I added an IsNumeric statement and defined the values globally and set them equal to 0. I managed to stop it from accepting numbers less than or equal to 0. I tried entering non-numeric characters only for it to throw out an exception. This happened when either the "Imperial (US)" or the "Metric" radio buttons were selected. Here is the updated code beginning with the declarations:
Dim decHeight As Decimal = 0
Dim decWeight As Decimal = 0
Dim decUS As Decimal = 0
Dim decMetric As Decimal = 0I decided to keep the decUS and decMetric decimal values in the code for now. These are closer to the beginning of the code. The updated version of the relevant code looks like this:
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
lblBMIHeader.Visible = True
lblBMI.Visible = True
If radUS.Checked = True Then 'The process that uses pounds and inches
decHeight = Convert.ToDecimal(txtHeight.Text)
decWeight = Convert.ToDecimal(txtWeight.Text)
'The "IsNumeric" statements should keep any non-numeric characters from becoming a problem.
'The rule for both Metric and Imperial measurements is that we want numbers greater than 0.
'Anything else should spit out an error message.
If IsNumeric(txtHeight.Text And txtWeight.Text) Then
If decHeight > 0 And decWeight > 0 Then
decUS = (decWeight / decHeight ^ 2) * 703
lblBMI.Text = decUS.ToString("00.0")
Else
MsgBox("Please try again. Only numbers greater than 0 should be in this field.", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
End If
Else
MsgBox("Please enter a positive number in the text boxes.", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
End If
ElseIf radMetric.Checked = True Then 'The process that uses kilograms and metres
decHeight = Convert.ToDecimal(txtHeight.Text)
decWeight = Convert.ToDecimal(txtWeight.Text)
If IsNumeric(txtHeight.Text And txtWeight.Text) Then
If decHeight > 0 And decWeight > 0 Then
decMetric = decWeight / decHeight ^ 2
lblBMI.Text = decMetric.ToString("00.0")
ElseIf (decHeight <= 0 And decWeight <= 0) Or (decHeight <= 0 Or decWeight <= 0) Then
MsgBox("Please enter a number greater than 0", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
End If
Else
MsgBox("Please enter a positive number in the text boxes.", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
End If
End If
End SubI'm a bit closer but I suspect I'm going to need to focus my attention elsewhere. This assignment requires that I add more things to the program (including a loop and a pictureBox object with 2 pictures in it). Wish me luck.
Update 2: I solved the problem I was running into where entering a non-numeric character, or nothing at all for that matter, would spit out an exception. The problem I ran into is that I had 1 IsNumeric handling input from both textboxes. I also moved the Convert statements below the Isnumeric statements. The new code looks like this:
'Here's where I attempt the BMI calculations. Wish me luck.
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
lblBMIHeader.Visible = True
lblBMI.Visible = True
If radUS.Checked = True Then 'The process that uses pounds and inches
'The "IsNumeric" statements should keep any non-numeric characters from becoming a problem.
'The rule for both Metric and Imperial measurements is that we want numbers greater than 0.
'Anything else should spit out an error message.
If IsNumeric(txtHeight.Text) And IsNumeric(txtWeight.Text) Then
decHeight = Convert.ToDecimal(txtHeight.Text)
decWeight = Convert.ToDecimal(txtWeight.Text)
If decHeight > 0 And decWeight > 0 Then
Else
MsgBox("Please try again. Only numbers greater than 0 should be in this field.", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
End If
decUS = (decWeight / decHeight ^ 2) * 703
lblBMI.Text = decUS.ToString("00.0")
Else
MsgBox("Please enter a positive number in the text boxes.", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
End If
ElseIf radMetric.Checked = True Then 'The process that uses kilograms and metres
If IsNumeric(txtHeight.Text) And IsNumeric(txtWeight.Text) Then
decHeight = Convert.ToDecimal(txtHeight.Text)
decWeight = Convert.ToDecimal(txtWeight.Text)
If decHeight > 0 And decWeight > 0 Then
ElseIf (decHeight <= 0 And decWeight <= 0) Or (decHeight <= 0 Or decWeight <= 0) Then
MsgBox("Please enter a number greater than 0", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
End If
decMetric = decWeight / decHeight ^ 2
lblBMI.Text = decMetric.ToString("00.0")
Else
MsgBox("Please enter a positive number in the text boxes.", 16, "Entry Invalid")
txtHeight.Text = ""
txtWeight.Text = ""
txtHeight.Focus()
txtWeight.Focus()
End If
End If
End SubNow I have to come up with a way to throw some pieces of garbage code into my program before noon tomorrow (and by "garbage code" I mean code that has garbage pieces in it that don't do anything). Wish me luck.