Try/Catch block for negative values

ewebb38

New member
Joined
Jan 21, 2005
Messages
2
Programming Experience
Beginner
Hi all I'm new to VB.net, and need a little help writing a try/catch block. I'm trying to catch any negative number entered into a text field called txtAmount.Text. If the number entered is negative a meesage box shows indicating a positive number needs entered.

Catch Err As Exception When (CInt(txtAmount.Text)) < 0
MessageBox.Show("Transaction Amount must be a positive number.", "Checkbook")
End Try

That's my code so far. What am I doing wrong? Instead of using the actual field, do I need to store the number that was entered in a temp variable first? Examples always appreciated.

Thanks for your help!
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Don't use a try catch, that's more for program errors. Use an IF statment.
E.G.
if CInt(txtAmount.Text)) < 0 THEN
MessageBox.Show("Transaction Amount must be a positive number.", "Checkbook")
END IF

You could wrap this if statment in a try catch incase the user enters text which would cause an error.

Hope that helps

TPM
 

ewebb38

New member
Joined
Jan 21, 2005
Messages
2
Programming Experience
Beginner
Thanks for clarifying that for me. I appreciate it.

I redid that protion and I now receive an error message when a negative value it input, however once I hit ok on the message box, the value I entered is then being put into the label I have to display it. What I would like to happen is when a message box appears alerting the user that an error occured, whatever was in the textbox should go away, and not be put into the label.

Please see attached print screen for clarification. You will see the error appears, but the negative value is still displaying.
 

Attachments

  • Error.doc
    41.5 KB · Views: 29

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
VB.NET:
IF IsNumeric(txtAmount.text) = true and Convert.ToInt32(txtAmount.Text) >= 0 THEN
 'rest of code from procedure
else
   MessageBox.Show("Transaction Amount must be a positive number.", "Checkbook")
END IF
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
As always in VB.NET there are many ways to accomplish this.

Do you want the messagebox display after the value is entered and the user moves the focus to another control or when the user presses the 'Calculate' button or some other time?
If you want to display the message when the focus changes, handle the validating event and set the CancelEventArgs.Cancel property to True. That will send the focus back to the textbox, but the entered text will remain. You could then select all the text to show the user that the text needs to be changed.
If you handle the keypress event for the textbox, you can make it so the user can not enter a minus sign in the textbox by setting the KeyPressEventArgs.Handled property to True.
See this thread for a way to allow only numeric values in the textbox: How to restrict TextBox?
 
Top Bottom