I have been given an assignment which says that i am supposed to
Write an Input validation, such that, the number of units for each package entered by the user will be only Numeric, and is not negative
Write a code to clear spaces before and after a text. (Hint: applying trimming)
At runtime, we cannot afford to allow the software to crash. You are to write appropriate error messages for the following:
a.When the user enters non-numeric value in any of the packages.
b.When the user enters negative value in any of the packages.
c.When the application suffers critical runtime error.
What I have tried:
Try
packageA = CInt(CDec(txtPackageA.Text))
packageB = CInt(CDec(txtPackageB.Text))
packageC = CInt(CDec(txtPackageC.Text))
Catch ex As Exception
MessageBox.Show("Numbers only and not Negative", "Input Error")
Return
End Try
If (packageA < 1) Then
MessageBox.Show("Numbers only and not Negative", " Input Error")
Return
End If
If (packageB < 1) Then
MessageBox.Show("Numbers only and not Negative", " Input Error")
Return
End If
If (packageC < 1) Then
MessageBox.Show(" Numbers only and not Negative", " Input Error")
Return
End If
lblAmountA.Text = " GHc" & packA.ToString
lblAmountB.Text += " GHc" & packB.ToString
lblAmountC.Text += " GHc" & packC.ToString
GrandTotal = CInt(packA + packB + packC)
lblTotal.Text += " GHc" & GrandTotal.ToString
Catch ex As Exception
End Try
End Sub
Private Sub txtPackageA_TextChanged(sender As Object, e As EventArgs) Handles txtPackageA.TextChanged
If (Not IsNumeric(txtPackageA.Text)) Then
ErrorProvider1.SetError(txtPackageA, "Only Numeric")
Else
ErrorProvider1.SetError(txtPackageA, "")
End If
End Sub
Private Sub txtPackageB_TextChanged(sender As Object, e As EventArgs) Handles txtPackageB.TextChanged
If (Not IsNumeric(txtPackageB.Text)) Then
ErrorProvider1.SetError(txtPackageB, "Only Numeric")
Else
ErrorProvider1.SetError(txtPackageB, "")
End If
End Sub
Private Sub txtPackageC_TextChanged(sender As Object, e As EventArgs) Handles txtPackageC.TextChanged
If (Not IsNumeric(txtPackageC.Text)) Then
ErrorProvider1.SetError(txtPackageC, "Only Numeric")
Else
ErrorProvider1.SetError(txtPackageC, "")
End If
End Sub
Private Sub txtPackageA_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtPackageA.Validating
If (String.IsNullOrEmpty(txtPackageA.Text)) Then
e.Cancel = True
ErrorProvider1.SetError(txtPackageA, "Please enter a Unit here!!!")
Else
e.Cancel = False
ErrorProvider1.SetError(txtPackageA, "")
End If
End Sub
Private Sub txtPackageB_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtPackageB.Validating
If (String.IsNullOrEmpty(txtPackageB.Text)) Then
e.Cancel = True
ErrorProvider1.SetError(txtPackageB, "Please enter a Unit here!!!")
Else
e.Cancel = False
ErrorProvider1.SetError(txtPackageB, "")
End If
End Sub
End Class