| Chapter 3- Data Types and Operators | Boolean Expressions Page 2 3 4 |
| The If Statement Page 2 3 4 5 6 7 8 9 | |
| Arithmetic Operations Page 2 3 4 5 6 | Boolean Operators and Nested If Statements Page 2 3 4 5 6 7 |
| Events and Sequential Processing Page 2 3 4 5 | More Examples Page 2 3 4 5 6 7 8 9 10 11 12 |
| Datatypes and Conversions Page 2 3 4 5 6 7 | Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10 |
| Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Exercises Page 2 3 4 5 6 7 8 |
| Chapter 4- Selection Statements | Review Questions |
| Introduction | |
4-7: Exercises
Exercise 4-9: The Validating Event and the CausesValidation Property
Validation of the data entered in the Age textbox(es) must occur, and this is where a new approach is introduced. It is often the case, as here, that we do not want the user to proceed interacting with the program until they have entered valid data - in this case the age data. Thus, when the user attempts to enter the age they will be forced to either enter a valid age, possibly leave it blank, clear the whole interface, or exit the program. They will not otherwise be able to click any button or option control, or enter any other text until they have correctly entered a valid age.
In this exercise the data entered in the age textbox is valid if it is the null string or numeric. If it is numeric the value must be greater than 0, and, if the Junior option is selected, less than 15.
Most controls have a property called CausesValidation
(examine the properties window for the control objects in this user
interface). In addition some controls (the Textbox control is relevant to our
exercise) have an event called Validating. This event
and the CausesValidation property work together. The
event is executed whenever focus shifts from the textbox to some other control
for which the CausesValidation property is set to True. If the CausesValidation
property is set to False of course the
Thus, in this exercise we will write code for the
This will mean that whenever the user types anything in one of the Age: textboxes and then interacts with any other object in the interface the Validating event will execute, checking whatever the user just typed.