
256
LESSON 21 Handling Errors
FIGURE 211
Lesson Requirements
Copy the program you built in Lesson 19, Exercise 4 (or download Lesson 19’s version from
the book’s web site).
The previous version of this program uses repeated code to perform calculations when the
user clicks either Calculate or OK. Move that code into a new ValuesAreOk method that
validates the user’s inputs. In addition to protecting the form from format errors, the func-
tion should verify that:
Item name is not blank.
Price Each > 0.
Quantity > 0.
If
ValuesAreOk finds a problem, it should:
Tell the user.
Set focus to the textbox that caused the problem.
Return
false.
If
ValuesAreOk finds that all of the values are okay, it should return true.
Hints
If the user clicks the OK button, the form should close only if the user’s inputs are valid. Be
sure the OK button’s DialogResult property doesn’t automatically close the form.
Use
try-catch blocks to protect against format errors.
Step-by-Step
Copy the program you built in Lesson 19, Exercise 4 (or download Lesson 19’s version from
the book’s web site).
1. This is straightforward.
596906book.indd 256 4/8/10 7:40:27 AM