Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 31-Jul-20 9:26am

1 solution

Instead of like that, get rid of the Try ... Catch code - you shouldn't rely on exceptions for "normal processing".
Instead, start by reading the instructions - and particularly the hint - and consider using the Int32.TryParse Method (System) | Microsoft Docs[^] which returns a boolean "I have converted it" / "It's not a valid number" once you have trimmed the input string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900