Something went wrong. Try again.
A collection of projects I've done for Berks Career and Technology Center Information Technology Programming
Something went wrong. Try again.
4.3 kB · 150 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151'Program Name: Weight Converter'Programmer : Ben Crocker'Date : 12/9/20'Purpose : Assist the user in converting a patient's weight to a different unit''Test Cases:''Case 1:'' > -5' < Input Must Be Positive!' 'Case 2:'' > 50 kg.' < ~110.250 lbs.''Case 3:'' > 110.250 lbs.' < ~50.000 kg.'
Option Strict On
Public Class WeightConvert
Private Sub cmbUnit_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbUnit.SelectedIndexChanged 'This code is run whenever the unit combox's value is changed 'It will only do something if there is an actual item selected 'It first gets the unit from the combobox 'It then sets the text of the button accordingly and enables it
If cmbUnit.SelectedIndex <> -1 Then
Dim strUnit As String = cmbUnit.SelectedItem.ToString() btnConvert.Text = "Convert To " & If(strUnit = "lbs.", "kg.", "lbs.") btnConvert.Enabled = True
End If
End Sub
Private Function FindErrors(strInput As String) As String 'This code is run to the the validity of the entered weight 'It first checks to make sure the value isnt empty 'It then checks to see that it can be converterted to a double 'Finally, if the data is valid, it returns 'No Error'
If strInput.Length > 0 Then
Try
Dim dblTestValue As Double = Convert.ToDouble(strInput)
If dblTestValue > 0 Then
Return "No Error"
Else
Return "Input Must Be Positive!"
End If
Catch fe As FormatException
Return "Value Must Be Numeric!"
Catch oe As OverflowException
Return "Value Is Too Big!"
End Try
Else
Return "Value Can't Be Empty!"
End If
End Function
Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click 'This code is run whenever the convert button is clicked 'It first gets the unit and the weight from the inputs 'It then validates the inputted weight 'If there are no errors, it continues, otherwise it will show the error to the user 'Then, it converts the weight to a double 'Next, it checks to see whether it should divide or multiply depending on the unit we're converting to 'Finally, it displays the converted weight and set sthe output label to visible
Dim strUnit As String = cmbUnit.SelectedItem.ToString() Dim strOppositeUnit As String = If(strUnit = "lbs.", "kg.", "lbs.") Dim strWeight As String = txtWeight.Text
Dim strError As String = FindErrors(strWeight)
If strError = "No Error" Then
Dim dblWeight As Double = Convert.ToDouble(strWeight) Dim dblConvertedWeight As Double
If strOppositeUnit = "lbs." Then
dblConvertedWeight = dblWeight * 2.20462
Else
dblConvertedWeight = dblWeight / 2.20462
End If
lblConvertedWeight.Text = dblConvertedWeight.ToString("F3") & " " & strOppositeUnit lblConvertedWeight.Visible = True
Else
MsgBox(strError,, "Input Error") lblConvertedWeight.Visible = False
End If
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click 'This code is run whenever the Clear button is clicked 'It first sets the selected index of the unit combobox to -1 to signify there is nothing in it 'It then disables and changes the text of the convert button 'Then, it hides the output label 'Finally, it clears the weight textbox
cmbUnit.SelectedIndex = -1 btnConvert.Enabled = False btnConvert.Text = "Please Select A Unit First" lblConvertedWeight.Visible = False txtWeight.Clear()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click 'This code is run whenever the exit button is clicked 'It exits the program
Close()
End Sub
End Class