WISE OWL EXERCISES
VISUAL BASIC EXERCISES
VISUAL BASIC EXERCISES
- Drawing forms (2)
- Form events (1)
- Variables and data types (2)
- Enumerations and constants (2)
- Form validation (3)
- Menus and toolbars (2)
- Files and folders (1)
- Basic DataGridViews (1)
- Reacting to DataGridView events (3)
- Advanced DataGridViews (2)
- Creating classes (4)
- Coding in VB.NET (2)
- Variables and constants (3)
- Testing conditions (2)
- Passing arguments (3)
- Using arrays (2)
- Loops (2)
- Working with files (3)
- Lists (3)
- Creating properties (3)
Visual Basic | Form validation exercise | Write code to validate text boxes
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
The answer to the exercise will be included and explained if you attend the relevant Wise Owl course (sadly, only in the UK for now).
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Add the existing form in the folder shown above into your project. When you run your application, you should see this:
You should call the ValidateChildren method from the APPLY button
Attach code to the Validating event of the txtName textbox, so that a message appears if you leave the name blank:
Display a message if a user leaves the name blank
Repeat this trick for the txtAge field, this time checking also that the age entered is a whole number (one way to do this is with :Try ... Catch ... End Try, within which you can attempt to convert the txtAge field's text property to an integer.
What you should see if you enter an invalid age (or none at all)
Finally, add code to the Age field's Validated event so that the age is increased by 10 years after it has been checked:
Reflecting the fact that everyone lies on dating agency application forms ...
You should now see that the Validated event fires whether you change the value in their Age text box or not. How might you stop your age increasing by 10 years every time a user leaves the Age text box?