I am trying to make a program that displays an error message in a StatusStrip control whenever the user doesn't enter a numeric value or a numeric value that falls within a certain range and gives the TextBox with aforementioned incorrect information focus after the user clicks the "Calculate" button. It displays the error message when the user fails to enter a numeric value in a TextBox but when the user enters a number outside the specified range it only gives focus to the TextBox and doesn't display the error message in the StatusStrip control. Please help.
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
' Variable declarations
Dim dblWeek1 As Double ' Week 1 Temperature
Dim dblWeek2 As Double ' Week 2 Temperature
Dim dblWeek3 As Double ' Week 3 Temperature
Dim dblWeek4 As Double ' Week 4 Temperature
Dim dblWeek5 As Double ' Week 5 Temperature
Dim dblFiveWeekSum As Double ' Sum of temperatures for five weeks
Dim dblAverage As Double ' Average of Temperatures
' Determine if there's a number in txtWeek1
If IsNumeric(txtWeek1.Text) Then
' Determine if the value is > -50 and < +130
If CDbl(txtWeek1.Text) > -50 And CDbl(txtWeek1.Text) < +130 Then
' Determine if there's a number in txtWeek2
If IsNumeric(txtWeek2.Text) Then
' Convert the text to a number and place it in a variable
dblWeek2 = CDbl(txtWeek2.Text)
' Determine if the value is > -50 and < +130
If dblWeek2 > -50 And dblWeek2 < 130 Then
' Display the temperature
txtWeek2.Text = dblWeek2.ToString()
' Determine if there's a number in txtWeek3
If IsNumeric(txtWeek3.Text) Then
' Convert the text to a number and place it in a variable
dblWeek3 = CDbl(txtWeek3.Text)
' Determine if the value is > -50 and < +130
If dblWeek3 > -50 And dblWeek3 < 130 Then
' Display the temperature
txtWeek3.Text = dblWeek3.ToString()
' Determine if there's a number in txtWeek4
If IsNumeric(txtWeek4.Text) Then
' Convert the text to a number and place it in a variable
dblWeek4 = CDbl(txtWeek4.Text)
' Determine if the value is > -50 and < +130
If dblWeek4 > -50 And dblWeek4 < 130 Then
' Display the temperature
txtWeek4.Text = dblWeek4.ToString()
' Determine if there's a number in txtWeek5
If IsNumeric(txtWeek5.Text) Then
' Convert the text to a number and place it in a variable
dblWeek5 = CDbl(txtWeek5.Text)
' Determine if the number is > -50 and < 130
If dblWeek5 > -50 And dblWeek5 < 130 Then
' Display the temperature
txtWeek5.Text = dblWeek5.ToString()
' Add the temperatures and store the answer in a variable
dblFiveWeekSum = dblWeek1 + dblWeek2 + dblWeek3 + dblWeek4 + dblWeek5
' Calculate the average
dblAverage = dblFiveWeekSum / 5
' Display the average
lblAverage.Text = dblAverage.ToString()
Else
lblStatus.Text = "Error: The temperature for Week 5 must be between -50 and 130."
txtWeek5.Focus()
End If
Else
lblStatus.Text = "Error: The Week 5 field must contain a numeric value."
txtWeek5.Focus()
End If
Else
lblStatus.Text = "Error: The temperature for Week 4 must be between -50 and 130."
txtWeek4.Focus()
End If
Else
lblStatus.Text = "The Week 4 field must contain a numeric value."
txtWeek4.Focus()
End If
Else
lblStatus.Text = "Error: The temperature for Week 3 must be between -50 and 130."
txtWeek3.Focus()
End If
Else
lblStatus.Text = "Error: The Week 3 field must contain a numeric value."
txtWeek3.Focus()
End If
Else
lblStatus.Text = "Error: The temperature for Week 2 must be between -50 and 130."
txtWeek2.Focus()
End If
Else
lblStatus.Text = "Error: The Week 2 field must contain a numeric value."
txtWeek2.Focus()
End If
Else
lblStatus.Text = "Error: The temperature for Week 1 must be between -50 and 130."
'MessageBox.Show("Error: The temperature for Week 1 must be between -50 and 130.")
txtWeek1.Focus()
End If
Else
lblStatus.Text = "Error: The Week 1 field must contain a numeric value."
txtWeek1.Focus()
End If
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
' Clear any error messages that may appear
lblStatus.Text = String.Empty
' Clear all the TextBox controls and the Label control.
txtWeek1.Clear()
txtWeek2.Clear()
txtWeek3.Clear()
txtWeek4.Clear()
txtWeek5.Clear()
lblAverage.Text = String.Empty
' Give focus to txtWeek1
txtWeek1.Focus()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Close the form
Me.Close()
End Sub
End Class
Aucun commentaire:
Enregistrer un commentaire