Sub Globals()
'for holding the actual real values, not the displayed values
Private bCopyEditText As Boolean
Private dHeightCm As Double
Private iHeightFeet As Int
Private dHeightInches As Double
Private dWeightKg As Double
Private iWeightStones As Int
Private dWeightPounds As Double
Private dBMI As Double
Private bMetric As Boolean
Private dFeet2Cm As Double = 30.48
Private dInch2Cm As Double = 2.54
Private dStone2Kg As Double = 6.35029
Private dPound2Kg As Double = 0.453592
Type tCalculator(BMI As Int, _
EDD As Int)
Private eCalculatorType As tCalculator
Type tImperialWeight(Stones As Int, _
Pounds As Double)
Type tImperialHeight(Feet As Int, _
Inches As Double)
eCalculatorType.BMI = 1
eCalculatorType.EDD = 2
Private iLastCalculator As Int
End Sub
Sub CInt(o As Object) As Int
Return Floor(o)
End Sub
Sub CDbl(o As Object) As Double
Return o
End Sub
Sub ShowBMICalculator
Dim bImperial As Boolean
If iLastCalculator = eCalculatorType.BMI Then
GotoPanel(ePanelType.Calculators,False,False)
lblACToolbarTitle.Text = "BMI"
lblPatCalculator.Text = GetPatLabelString(-1)
Return
End If
bCopyEditText = True '<<<<
If pnlCalculators.IsInitialized Then
pnlCalculators.RemoveAllViews
End If
If pnlCalculators.IsInitialized = False Then
Activity.LoadLayout("Calculators")
arrPanels(ePanelType.Calculators) = pnlCalculators
End If
pnlCalculators.LoadLayout("BMI")
GotoPanel(ePanelType.Calculators,False,False)
lblACToolbarTitle.Text = "BMI"
lblPatCalculator.Text = GetPatLabelString(-1)
General.RunLog("ShowBMICalculator, bMetric: " & bMetric)
bImperial = bMetric = False
edtHeight2.Visible = bImperial
edtWeight2.Visible = bImperial
lblInch.Visible = bImperial
lblPounds.Visible = bImperial
chkMetric.Checked = bMetric
If bMetric Then
lblCm.Text = "Cm"
lblKg.Text = "Kg"
Else
lblCm.Text = "Feet"
lblKg.Text = "Inches"
End If
iLastCalculator = eCalculatorType.BMI
End Sub
Sub MetricHeight2Imperial(iCm As Int) As tImperialHeight
Dim tIH As tImperialHeight
General.RunLog("MetricHeight2Imperial, Round2(1.6, 0): " & Round2(1.6, 0))
tIH.Feet = CInt(iCm / dFeet2Cm)
tIH.Inches = (iCm - tIH.Feet * dFeet2Cm) / dInch2Cm
Return tIH
End Sub
Sub MetricWeight2Imperial(dKg As Double) As tImperialWeight
Dim tIW As tImperialWeight
tIW.Stones = CInt(dKg / dStone2Kg)
tIW.Pounds = (dKg - tIW.Stones * dStone2Kg) / dPound2Kg
Return tIW
End Sub
Sub ImperialWeight2Kg(iStones As Int, dPounds As Double) As Double
Return iStones * dStone2Kg + dPounds * dPound2Kg
End Sub
Sub ImperialHeight2Cm(iFeet As Int, dInches As Double) As Double
Return iFeet * dFeet2Cm + dInches * dInch2Cm
End Sub
Sub btnCalculateBMI_Click
If edtWeight.Text.Length = 0 Then
Return
End If
If edtHeight.Text.Length = 0 Then
Return
End If
If bMetric Then
dBMI = dWeightKg / Power(dHeightCm / 100, 2)
Else
dBMI = ImperialWeight2Kg(iWeightStones, dWeightPounds) / Power(ImperialHeight2Cm(iHeightFeet, dHeightInches) / 100, 2)
End If
bCopyEditText = False
edtBMI.Text = Round2(dBMI, 1)
End Sub
Sub btnCalculateForBMI_Click 'calculate the weight to get BMI of x
Dim tIW As tImperialWeight
If edtBMI.Text.Length = 0 Then
Return
End If
If edtHeight.Text.Length = 0 Then
Return
End If
If bMetric Then
dWeightKg = dBMI * Power(dHeightCm / 100, 2)
Else
dWeightKg = dBMI * Power(ImperialHeight2Cm(iHeightFeet, dHeightInches) / 100, 2)
End If
If bMetric Then
bCopyEditText = False
edtWeight.Text = Round2(dWeightKg, 1)
Else
tIW = MetricWeight2Imperial(dWeightKg)
edtWeight.Text = tIW.Stones
bCopyEditText = False
edtWeight2.Text = Round2(tIW.Pounds, 1)
iWeightStones = tIW.Stones
dWeightPounds = tIW.Pounds
End If
End Sub
'to retain the real values, when entering directly in the edittext
Sub edtHeight_TextChanged(Old As String, New As String)
If bCopyEditText Then
If bMetric Then
If New.Length > 0 Then
dHeightCm = New
Else
dHeightCm = 0
End If
Else
If New.Length > 0 Then
iHeightFeet = New
Else
iHeightFeet = 0
End If
End If
End If
bCopyEditText = True
End Sub
Sub edtHeight2_TextChanged(Old As String, New As String)
If bCopyEditText Then
If New.Length > 0 Then
dHeightInches = New
Else
dHeightInches = 0
End If
End If
bCopyEditText = True
End Sub
Sub edtWeight_TextChanged(Old As String, New As String)
If bCopyEditText Then
If bMetric Then
If New.Length > 0 Then
dWeightKg = New
Else
dWeightKg = 0
End If
Else
If New.Length > 0 Then
iWeightStones = New
Else
iWeightStones = 0
End If
End If
End If
bCopyEditText = True
End Sub
Sub edtWeight2_TextChanged(Old As String, New As String)
If bCopyEditText Then
If New.Length> 0 Then
dWeightPounds = New
Else
dWeightPounds = 0
End If
End If
bCopyEditText = True
End Sub
Sub edtBMI_TextChanged(Old As String, New As String)
If bCopyEditText Then
If New.Length> 0 Then
dBMI = New
Else
dBMI = 0
End If
End If
bCopyEditText = True
End Sub
Sub chkMetric_Click() 'switch between Imperial and Metric
Dim bImperial As Boolean
Dim tIW As tImperialWeight
Dim tIH As tImperialHeight
bMetric = chkMetric.Checked
bImperial = bMetric = False
edtHeight2.Visible = bImperial
edtWeight2.Visible = bImperial
lblInch.Visible = bImperial
lblPounds.Visible = bImperial
If bMetric Then
lblCm.Text = "Cm"
lblKg.Text = "Kg"
If edtHeight.Text.Length > 0 Or edtHeight2.Text.Length > 0 Then
dHeightCm = (iHeightFeet * dFeet2Cm + dHeightInches * dInch2Cm)
bCopyEditText = False
edtHeight.Text = Round2(dHeightCm, 1)
End If
If edtWeight.Text.Length > 0 Or edtWeight2.Text.Length > 0 Then
dWeightKg = iWeightStones * dStone2Kg + dWeightPounds * dPound2Kg
bCopyEditText = False
edtWeight.Text = Round2(dWeightKg, 1)
End If
Else
lblCm.Text = "Feet"
lblKg.Text = "Stone"
If edtHeight.Text.Length > 0 Then
tIH = MetricHeight2Imperial(dHeightCm)
edtHeight.Text = tIH.Feet
bCopyEditText = False
edtHeight2.Text = Round2(tIH.Inches, 1)
iHeightFeet = tIH.Feet
dHeightInches = tIH.Inches
End If
If edtWeight.Text.Length > 0 Then
tIW = MetricWeight2Imperial(dWeightKg)
edtWeight.Text = tIW.Stones
bCopyEditText = False
edtWeight2.Text = Round2(tIW.Pounds,1)
iWeightStones = tIW.Stones
dWeightPounds = tIW.Pounds
End If
End If
If (edtWeight.Text.Length > 0 And edtHeight.Text.Length > 0) Then
btnCalculateBMI_Click
End If
SaveSetting("Metric", bMetric)
End Sub