Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.8 kB
3
Indexable
Never
Public Class MainForm
    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 Not Double.TryParse(temperatures(i), temp) Then
                ' 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."
            ElseIf temp < -50 OrElse temp > 130 Then
                ' 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."
            Else
                sum += temp
            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

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        ' Clear the TextBoxes, Labels, and StatusStrip
        txtWeek1.Clear()
        txtWeek2.Clear()
        txtWeek3.Clear()
        txtWeek4.Clear()
        txtWeek5.Clear()
        lblAverage.Text = ""
        statusLabel.Text = ""
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        ' Close the application
        Me.Close()
    End Sub
End Class