Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
1
Indexable
Never
Private Sub btnCalculateAverage_Click(sender As Object, e As EventArgs) Handles btnCalculateAverage.Click
    Dim temperatures(4) As String
    Dim sum As Double = 0
    Dim average As Double = 0
    Dim isValid As Boolean = True

    ' Store the TextBox values in the temperatures array
    temperatures(0) = txtWeek1.Text
    temperatures(1) = txtWeek2.Text
    temperatures(2) = txtWeek3.Text
    temperatures(3) = txtWeek4.Text
    temperatures(4) = txtWeek5.Text

    ' Check the input and calculate the sum
    For i As Integer = 0 To 4
        Dim temp As Double

        If Double.TryParse(temperatures(i), temp) Then
            If temp >= -50 AndAlso temp <= 130 Then
                sum += temp
            Else
                ' Set isValid to False if the temperature is out of range
                isValid = False
                ' Display an error message in the StatusStrip
                statusLabel.Text = "Error: Invalid temperature for Week #" & (i + 1).ToString() & ". Please enter a value between -50 and 130."
            End If
        Else
            ' Set isValid to False if the input is non-numeric
            isValid = False
            ' Display an error message in the StatusStrip
            statusLabel.Text = "Error: Non-numeric input for Week #" & (i + 1).ToString() & ". Please enter a numeric value."
        End If

        If Not isValid Then
            ' Set focus to the incorrect field
            Select Case i
                Case 0
                    txtWeek1.Focus()
                Case 1
                    txtWeek2.Focus()
                Case 2
                    txtWeek3.Focus()
                Case 3
                    txtWeek4.Focus()
                Case 4
                    txtWeek5.Focus()
            End Select
            ' Exit the loop
            Exit For
        End If
    Next

    If isValid Then
        ' Calculate the average
        average = sum / 5
        ' Display the average
        lblAverage.Text = average.ToString("N2")
        ' Clear the StatusStrip
        statusLabel.Text = ""
    End If
End Sub